我想在mysql中使用sum語句,但沒有運氣。以下是我的查詢,我只想在銷售訂單編號字段和項目匹配時將生產訂單中的數量字段加起來,下面的查詢只返回一行,並將迄今爲止發佈的所有生產訂單數量相加。我確實有大約2000個銷售訂單,並且發佈了與銷售訂單有關的3500個生產訂單。SQL總結聲明奇怪地工作
任何幫助將真的成爲一個救星。
select
`tabSales Order`.`name` as "Sales Order:Link/Sales Order:120",
`tabSales Order`.`customer` as "Customer:Link/Customer:120",
`tabSales Order Item`.item_code as "Item:Link/Item:120",
`tabSales Order Item`.item_name as "Des:Link/Item:120",
`tabSales Order Item`.qty as "Qty:Float:100",
`tabSales Order Item`.delivered_qty as "Delivered Qty:Float:100",
(`tabSales Order Item`.qty - ifnull(`tabSales Order Item`.delivered_qty, 0)) as "Qty to Deliver:Float:140",
`tabSales Order Item`.date as "Sch date:Date:100",
`tabItem`.thumbnail as "Cut wt:Data:60",
`tabProduct master`.grade as "Grade:Data:100",
`tabProduct master`.dieno as "Die no:Data:100",
`tabProduct master`.hammer as "Hammer:Data:50",
`tabProduct master`.rm_idl_cs as "Idl sec:Data:60",
`tabProduct master`.rm_alt_cs as "Alt sec:Data:60",
SUM(CASE WHEN `tabSales Order`.`name` = `tabProduction Order`.`sales_order` and `tabSales Order Item`.`item_code` = `tabProduction Order`.`production_item`
THEN `tabProduction Order`.`qty`
END) as "Issued:Int:80"
from
`tabSales Order`
LEFT JOIN `tabSales Order Item` ON (`tabSales Order Item`.`parent` = `tabSales Order`.`name`
and `tabSales Order`.docstatus = 1 and `tabSales Order`.status not in ("Stopped", "Closed") and ifnull(`tabSales Order Item`.delivered_qty,0) < ifnull(`tabSales Order Item`.qty,0))
LEFT JOIN `tabItem` ON `tabItem`.`item_code` = `tabSales Order Item`.`item_code`
LEFT JOIN `tabProduct master` ON `tabProduct master`.`part_no` = `tabSales Order Item`.`item_code`
LEFT JOIN `tabProduction Order` ON (`tabSales Order Item`.`item_code` = `tabProduction Order`.`production_item` and `tabSales Order`.`name` = `tabProduction Order`.`sales_order`)
在此先感謝
何處是你組的聲明? – Cherif
Ouch,你正在過濾'on'子句中左連接的左側表,這是不行的。這樣的過濾器應該在where子句中。 – Shadow
您的整個查詢對我來說沒有任何意義。請根據樣本數據提供樣本數據和預期輸出。此刻,我根本不明白你在做什麼。 – Shadow