我正在尝试使用类别中供应商的结果显示矩阵表。像:
+-----------+-------------+-------------+------+
| | 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)
,但这给了我办公室中所有供应商的总结果。
其次,我尝试使用子查询。但这给了我一个错误,说明未知列。
我想要达到的结果是:
+-----------+--------- -------+
| Vendor | Results |
+-----------+-----------------+
| Vendor 1 | 8900,0,... |
| Vendor 2 | 56890,4000,... |
| ... | ...,...,... |
+-----------+-----------------+
我是在尝试一些不切实际的东西,我是否应该为每个供应商执行上述查询。或者我在这里错过了什么?
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
答案 0 :(得分:0)
您可能需要加入vendor表以获得所需的供应商(而不是单个供应商)。你还需要在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
答案 1 :(得分: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;