2014-09-25 170 views
2

我非常困惑這個GROUP BY &聯合聲明。Group by&Union In Sql

現在我得到這個O/P。

enter image description here

但我想我的輸出看起來像這樣:

Vendor Name   Purchase Order   Req Qty  Inward Qty 
    ----------------------------------------------------------------------  
    Philips Lighting  PO20140918133011  350    350 
    (I) Pvt Ltd 

要求:

如果我的供應商爲null,則也是我必須GROUP BY的基礎上,採購訂單。 但是得到的所有的要件不滿足的輸出有沒有什麼辦法能根據採購訂單做小組。

**查詢:**

SELECT 
    vendor_name, 
    purchase_order_no, 
    SUM(ISNULL(InwardQty,0)) AS InwardQty, 
    SUM(ISNULL(ReqQty,0)) ReqQty 
FROM 
(
    SELECT 
     vendor_name, 
     pm.purchase_order_no, 
     ISNULL(SUM(PIIM.qty),0) AS ReqQty, 
     0 InwardQty 
    FROM 
     RS_Purchase_Order_Master AS PM 
     LEFT OUTER JOIN RS_Purchase_Invoice_Info_Master AS PIIM ON PIIM.purchase_order_no = PM.purchase_order_no 
     LEFT OUTER JOIN RS_Vendor_Master AS VM ON VM.vendor_id = PM.vendor_id 
    WHERE 
     IsPicreated = 1 
    GROUP BY 
     vendor_name, 
     pm.purchase_order_no 

    UNION 

    SELECT 
     NULL AS vendor_name, 
     purchase_order_no, 
     0 AS ReqQty, 
     ISNULL(SUM(qty),0) AS InwardQty 
    FROM 
     RS_GIN_Master  
    GROUP BY 
     purchase_order_no 
) 
AS A 
GROUP BY 
    vendor_name, 
    purchase_order_no 

任何幫助或建議將不勝感激。

預先感謝您

+1

你應該扭轉你的'ISNULL(SUM('如果一個值爲空,但其他人都沒有,總和將返回null,你的整個價值將爲0'SUM (ISNULL(InwardQTY,0))' – paqogomez 2014-09-25 07:09:11

+0

thanx對於該建議,先生paqogomez。但我的主要問題是,我得到兩行,因爲供應商列中的空值應該如何將兩行轉換爲一行 – 2014-09-25 07:11:35

+1

那就是爲什麼它是一個評論,而不是一個答案,但它會在你的結果中出現問題。你的主要問題是因爲你是按vendor_name分組的。菲利普如果這是有道理的) ou應該通過PO刪除vendor_name,group,然後回去獲取供應商名稱。 – paqogomez 2014-09-25 07:15:08

回答

1

您不需要創建臨時表。只需從GROUP BY中刪除vendor_name,然後添加MAX(vendor_name)。

SELECT 
    MAX(vendor_name) AS vendor_name, 
    purchase_order_no, 
    SUM(ISNULL(InwardQty,0)) AS InwardQty, 
    SUM(ISNULL(ReqQty,0)) AS ReqQty 
FROM 
(
    SELECT 
     vendor_name, 
     pm.purchase_order_no, 
     ISNULL(SUM(PIIM.qty),0) AS ReqQty, 
     0 InwardQty 
    FROM 
     RS_Purchase_Order_Master AS PM 
     LEFT OUTER JOIN RS_Purchase_Invoice_Info_Master AS PIIM ON PIIM.purchase_order_no = PM.purchase_order_no 
     LEFT OUTER JOIN RS_Vendor_Master AS VM ON VM.vendor_id = PM.vendor_id 
    WHERE 
     IsPicreated = 1 
    GROUP BY 
     vendor_name, 
     pm.purchase_order_no 

    UNION 

    SELECT 
     NULL AS vendor_name, 
     purchase_order_no, 
     0 AS ReqQty, 
     ISNULL(SUM(qty),0) AS InwardQty 
    FROM 
     RS_GIN_Master  
    GROUP BY 
     purchase_order_no 
) 
AS A 
GROUP BY 
    purchase_order_no 
1

我不知道這是不是最好的解決辦法,但我會創建一個臨時表(@a):

declare @a table (
purchase_order_no varchar(100), 
TotalReqQty int, 
TotalInwardQty int) 

插入的結果:

SELECT purchase_order_no, 
    SUM(ReqQty), 
    SUM(InwardQty) 
FROM table 
group by purchase_order_no 

然後,我會使用內部連接來獲得供應商使用這樣的東西:

SELECT T.Vendor_Name, 
    A.purchase_order_no, 
    A.TotalReqQty, 
    A.TotalInwardQty 
FROM @a a 
inner join 
(SELECT DISTINCT Vendor_Name, purchase_order_no 
    FROM table WHERE Vendor_Name IS NOT NULL) T 
    ON A.purchase_order_no = T.purchase_order_no 
1
如果我看看你的查詢

聯盟的子查詢 - >讓每個purchase_order_no表RS_GIN_Master總和(數量)

子查詢 - >使2行是單排。

試試這個:

SELECT 
    vendor_name, 
    pm.purchase_order_no, 
    ISNULL(SUM(PIIM.qty),0) AS ReqQty, 
    sum(xx.InwardQty) as InwardQty 
FROM 
    RS_Purchase_Order_Master AS PM 
    LEFT OUTER JOIN RS_Purchase_Invoice_Info_Master AS PIIM ON PIIM.purchase_order_no =  PM.purchase_order_no 
    LEFT OUTER JOIN RS_Vendor_Master AS VM ON VM.vendor_id = PM.vendor_id 
    LEFT JOIN (
       SELECT purchase_order_no, ISNULL(SUM(qty),0) AS InwardQty 
       FROM RS_GIN_Master 
       GROUP BY purchase_order_no 
      ) xx On xx.purchase_order_no = pm.purchase_order_no 
WHERE 
    IsPicreated = 1 
GROUP BY 
    vendor_name, 
    pm.purchase_order_no