2017-05-02 49 views
-1

我有一個問題,我的下面的查詢,銷售訂單項是銷售訂單表內的子表,這些表(銷售訂單項)有多個行只顯示第一項我的意思是第一行,所有其他行都沒有得到顯示。請擺脫一些燈光傢伙。問題與子表 - 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; 
+0

看看您是否可以使用'HAVING IFNULL(...- SUM(...),1)'而不是計算兩次。 –

+0

@RickJames。嘗試使用HAVING IFNULL(..),但問題仍然存在。任何幫助?謝謝 – Ragav

+0

對不起,這只是一個簡化的建議,而不是一個解決方案。 –

回答

0

首先,縮短它,使之更具可讀性:

FROM `tabSales Order` AS so 
    LEFT JOIN `tabSales Order Item` AS soi 
       ON (soi.`parent` = so.`name` 
       AND so.docstatus = 1 ) 
    LEFT JOIN `tabItem` AS i ON i.`item_code` = soi.`item_code` 
    LEFT JOIN `tabProduct master` AS pm 
       ON pm.`part_no` = soi.`item_code` 
    LEFT JOIN `tabProduction Order` AS po 
       ON (soi.`item_code` = po.`production_item` 
       AND so.`name` = po.`sales_order`) 
    WHERE so.status NOT IN ("Stopped", "Closed", "Completed") 
     AND IFNULL(soi.delivered_qty,0) < IFNULL(soi.qty, 0) 
    GROUP BY so.`name` 
    HAVING IFNULL(soi.qty - 
       SUM(CASE WHEN so.`name` = po.`sales_order` 
        AND soi.`item_code` = po.`production_item` 
         THEN po.`qty` END), 1) > 0 

然後一些一般性的分析,而不必解決問題:

  • 你真的需要LEFT?也就是說,「正確」表是可選的,你想要得到的NULLs
  • LEFT JOIN b ON b.x = 1LEFT JOIN b WHERE b.x = 1之間的差異。前者可能比後者包含更多的b行。
  • 擁有NULLs在許多情況下都很好,但如果可以避免它們,SQL就會變得更簡單。

開始運行它沒有 「GROUP BY tabSales Ordername」。你會得到你缺失的線條,但你可能會得到比你想要的更多。如果是這樣,然後刪除其餘的連接,讓我們看看我們是否可以將它們添加回來沒有項目的「內爆」。

爲了幫助理解數據,請說明每個關係是1:1還是1:多個還是多個:許多。

+0

FWIW,我願意與's','si'和'我' – Strawberry

+0

感謝您的回覆@Rick James。是的,我知道,但我需要在我的情況下有NULL。任何想法爲什麼我無法獲得子表中的所有行? – Ragav

+0

哪個表是「子表」? –