我正在尝试显示一个矩阵表,其中包含某个类别中供应商的结果。喜欢:
+-----------+-------------+-------------+------+
| | 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