我有一個問題,我的下面的查詢,銷售訂單項是銷售訂單表內的子表,這些表(銷售訂單項)有多個行只顯示第一項我的意思是第一行,所有其他行都沒有得到顯示。請擺脫一些燈光傢伙。問題與子表 - MySql查詢
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:Int:100",
`tabSales Order Item`.delivered_qty as "Delivered Qty:Int:100",
(`tabSales Order Item`.qty - ifnull(`tabSales Order Item`.delivered_qty, 0)) as "Qty to Deliver:Int:140",
`tabSales Order Item`.date as "Sch date:Date:100",
`tabItem`.thumbnail as "Cut wt:Data:60",
(`tabSales Order Item`.qty * `tabItem`.thumbnail) as "Tonnage:Float:80",
`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",
ifnull(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),0) as "Issued:Int:80",
SUM(CASE WHEN `tabSales Order`.`name` = `tabProduction Order`.`sales_order` and `tabSales Order Item`.`item_code` = `tabProduction Order`.`production_item`
THEN `tabProduction Order`.`produced_qty`
END) as "Cut:Int:80",
(`tabSales Order Item`.qty - ifnull(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),0)) as "To be issued:Int:80"
from `tabSales Order`
LEFT JOIN `tabSales Order Item` ON (`tabSales Order Item`.`parent` = `tabSales Order`.`name`
and `tabSales Order`.docstatus = 1 )
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`)
where `tabSales Order`.status not in ("Stopped", "Closed", "Completed")
and ifnull(`tabSales Order Item`.delivered_qty,0) < ifnull(`tabSales Order Item`.qty, 0)
GROUP BY `tabSales Order`.`name`
HAVING (`tabSales Order Item`.qty -
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)
) > 0
or (`tabSales Order Item`.qty -
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)
) is NULL;
看看您是否可以使用'HAVING IFNULL(...- SUM(...),1)'而不是計算兩次。 –
@RickJames。嘗試使用HAVING IFNULL(..),但問題仍然存在。任何幫助?謝謝 – Ragav
對不起,這只是一個簡化的建議,而不是一個解決方案。 –