我试图在mysql中使用sum语句,但没有运气。下面是我的查询,我只想在生产订单中的qty字段在销售订单编号字段和项目匹配时加起来,下面的查询只返回一行,累加到目前为止发布的所有生产订单数量。我确实有大约2000份销售订单和3500份与销售订单有关的生产订单。
任何帮助都会成为一个救生员。
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`)
提前致谢
答案 0 :(得分:0)
如果你查询只返回一行,那是因为你使用的是聚合函数而没有group by语句。
如果你想根据聚合函数设置条件,你应该使用
having
之后group by
group by col1, col2,..
having sum(col1) > 1