我有这个问题:
select item_code, item_name, First, Second, Third, `Fourth` from (
(SELECT t.item_code, i.item_name, t.actual_qty AS `First`, '' AS `Second`, '' AS `Third`, '' AS `Fourth` FROM tabBin t JOIN `tabItem` i ON i.name = t.item_code WHERE t.warehouse = "Finished Goods")
UNION
(SELECT t.item_code, i.item_name, '' AS `First`, t.actual_qty AS `Second`, '' AS `Third`, '' AS `Fourth` FROM tabBin t JOIN `tabItem` i ON i.name = t.item_code WHERE t.warehouse = "Tank 01 - 1200 KG")
UNION
(SELECT t.item_code, i.item_name, '' AS `First`, '' AS `Second`, t.actual_qty AS `Third`, '' AS `Fourth` FROM tabBin t JOIN `tabItem` i ON i.name = t.item_code WHERE t.warehouse = "Tank 02 - 1200 KG")
UNION
(SELECT t.item_code, i.item_name, '' AS `First`, '' AS `Second`, '' AS `Third`, t.actual_qty AS `Fourth` FROM tabBin t JOIN `tabItem` i ON i.name = t.item_code WHERE t.warehouse = "Tank 03 - 1200 KG")) as temp
GROUP BY temp.item_code, temp.item_name, temp.First, temp.Second, temp.Third, temp.Fourth
这是输出:
item_code item_name first, second, third, fourth
fg-plu PLUM 30.000000
fg-plu PLUM 40.000000
fg-plu PLUM 10.000000
fg-plu PLUM 1248.00
我希望按item_code
和item_name
或item_code
进行分组。
答案 0 :(得分:0)
这实际上似乎是伪装的透视查询。只需将CASE
与SELECT
t.item_code,
i.item_name,
MAX(CASE WHEN t.warehouse='Finished Goods' THEN t.actual_qty END) AS first,
MAX(CASE WHEN t.warehouse='Tank 01 - 1200 KG' THEN t.actual_qty END) AS second,
MAX(CASE WHEN t.warehouse='Tank 02 - 1200 KG' THEN t.actual_qty END) AS third,
MAX(CASE WHEN t.warehouse='Tank 03 - 1200 KG' THEN t.actual_qty END) AS fourth
FROM tabBin t
INNER JOIN tabItem i
ON i.name = t.item_code
GROUP BY
t.item_code,
i.item_name
表达式一起使用即可获得所需的输出:
C:\Users\powershell> .\test.ps1 -flag
答案 1 :(得分:0)
解决了
enter code Select i.item_name,IFNULL((select b.actual_qty as i2total
from `tabItem` as i2
join `tabBin` as b on b.item_code = i2.item_code AND i2.item_code = i2.item_code
where b.warehouse = "Finished Goods" AND i2.item_code = i.item_code),0) foa, IFNULL((select b.actual_qty as i2total
from `tabItem` as i2
join `tabBin` as b on b.item_code = i2.item_code AND i2.item_code = i2.item_code
where b.warehouse = "Toros" AND i2.item_code = i.item_code),0) toros, (foa + toros) AS total FROM `tabItem` as i WHERE i.item_group = "products" GROUP BY i.item_name, foa, toros ;
感谢大家的帮助。