0

我正在尝试显示一个矩阵表,其中包含某个类别中供应商的结果。喜欢:

+-----------+-------------+-------------+------+
|           | Category 1  | Category 2  | ...  |
+-----------+-------------+-------------+------+
| Vendor 1  |       8900  |          0  | ...  |
| Vendor 2  |      56890  |       4000  | ...  |
|      ...  |        ...  |        ...  | ...  |
+-----------+-------------+-------------+------+

我已经查询了一个供应商:

SELECT ut.unit_name, IFNULL(SUM(commission_fix_out),0)
FROM transaction t
JOIN office_per_transaction opt
ON t.id = opt.transaction_id
JOIN_vendor_per_transaction ppt
ON t.id = ppt.transaction_id
AND ppt.vendor_id = 2
RIGHT JOIN unit_type ut
ON t.unit_type_id = ut.id
AND transaction_end_week BETWEEN 0 AND 14
AND YEAR(transaction_end_date) = 2012
GROUP BY ut.id

这给了我以下结果:

+-------------+---------+
| Category    | Result  |
+-------------+---------+
| Category 1  |  56890  |
| Category 2  |   4000  |
|        ...  |    ...  |
+-------------+---------+

现在我想查看给定办公室中每个供应商的此查询的串联。

首先,我尝试替换AND ppt.vendor_id = 2为,AND ppt.vendor_id IN (SELECT id FROM vendor WHERE office_id = 1)但这给了我该办公室所有供应商的总结果。

其次,我尝试使用子查询。但这给了我一个错误,说明unknown column

我想要实现的是一个结果集,例如:

+-----------+--------- -------+
| Vendor    | Results         |
+-----------+-----------------+
| Vendor 1  | 8900,0,...      |
| Vendor 2  | 56890,4000,...  |
| ...       | ...,...,...     |
+-----------+-----------------+

我是否正在尝试一些不切实际的事情,我是否应该对每个供应商执行上述查询。或者我在这里错过了什么?

@SashiKant我最近几天尝试了很多查询,但它是这样的:

SELECT v.id, GROUP_CONCAT(r.result) FROM vendor v, 
   ( SELECT ut.unit_name, IFNULL(SUM(commission_fix_out),0) as result 
     FROM transaction t JOIN office_per_transaction opt ON t.id = opt.transaction_id 
     JOIN vendor_per_transaction ppt ON t.id = ppt.transaction_id 
     AND ppt.vendor_id = p.id 
     RIGHT JOIN unit_type ut ON unit_type_id = ut.id 
     AND transaction_end_week BETWEEN 0 AND 14 
     AND YEAR(transaction_end_date) = 2012 GROUP BY ut.id) as r
4

2 回答 2

0

您可能需要加入供应商表以获取您想要的供应商(而不是单个供应商)。此外,您需要在 GROUP BY 中使用更多子句(并且您确实应该在 GROUP BY 子句中使用 SELECT 中的所有非聚合列 - mysql 不反对,但如果您错过了一些,大多数 SQL 都会反对)。

像这样的东西(未经测试,所以请原谅任何错别字)

SELECT vendor_id, ut.id, ut.unit_name, IFNULL(SUM(commission_fix_out),0)
FROM transaction t
INNER JOIN office_per_transaction opt
ON t.id = opt.transaction_id
INNER JOIN_vendor_per_transaction ppt
ON t.id = ppt.transaction_id
INNER JOIN vendor ve
ON ve.id = ppt.vendor_id 
RIGHT JOIN unit_type ut
ON t.unit_type_id = ut.id
AND transaction_end_week BETWEEN 0 AND 14
AND YEAR(transaction_end_date) = 2012
WHERE ve.office_id = 1
GROUP BY vendor_id, ut.id, ut.unit_name
ORDER BY vendor_id, ut.id, ut.unit_name

如果您希望每个供应商有一行,并且该供应商的所有金额只有一列,那么就像这样。

SELECT vendor_id, GROUP_CONCAT(CommisionAmount)
FROM (
SELECT vendor_id, ut.id, ut.unit_name, IFNULL(SUM(commission_fix_out),0) AS CommisionAmount
FROM transaction t
INNER JOIN office_per_transaction opt
ON t.id = opt.transaction_id
INNER JOIN_vendor_per_transaction ppt
ON t.id = ppt.transaction_id
INNER JOIN vendor ve
ON ve.id = ppt.vendor_id 
RIGHT JOIN unit_type ut
ON t.unit_type_id = ut.id
AND transaction_end_week BETWEEN 0 AND 14
AND YEAR(transaction_end_date) = 2012
WHERE ve.office_id = 1
GROUP BY vendor_id, ut.id, ut.unit_name
ORDER BY vendor_id, ut.id, ut.unit_name) Sub1 
于 2012-11-27T09:36:34.853 回答
0

您可以使用group_concat.

伪 SQL:

select vendor, group_concat(result)
from mytable
group by vendor

您可以通过创建视图来“简化”它。由于我不知道您的表结构,这只是猜测

create view commissions as
    SELECT ppt.vendor_id as vendor, ut.unit_name as category,
           IFNULL(SUM(commission_fix_out),0) as commission
    FROM transaction t
    JOIN office_per_transaction opt
    ON t.id = opt.transaction_id
    JOIN_vendor_per_transaction ppt
    ON t.id = ppt.transaction_id
    RIGHT JOIN unit_type ut
    ON t.unit_type_id = ut.id
    AND transaction_end_week BETWEEN 0 AND 14
    AND YEAR(transaction_end_date) = 2012
    GROUP BY ppt.vendor_id, ut.id;

然后选择

select vendor, group_concat(commission)
from commissions
group by vendor;
于 2012-11-27T09:37:38.217 回答