2014-09-30 17 views
0

此線程與我的舊版本相關Combine fields from different rows on conditionJOINs困惑 - 結果中的數據缺失

我調整了從該線程獲得的查詢以滿足其他一些要求。

SELECT 
    a.Date, 
    a.orderid AS AZNr, 
    a.Typ, 
    ROUND(a.Fees, 2) AS Fees, 
    ROUND(b.Shipping, 2) AS Shipping, 
    ROUND(c.Price, 2) AS Price, 
    d.DeliveryLand 
FROM 
    (SELECT 
     posteddate AS Date, 
      transactiontype AS Typ, 
      orderid, 
      SUM(amount) AS Fees 
    FROM 
     report 
    WHERE 
     amounttype = 'ItemFees' 
    GROUP BY orderid) a 
     LEFT JOIN 
    (SELECT 
     orderid, SUM(amount) AS Shipping 
    FROM 
     report 
    WHERE 
     amountdescription = 'Shipping' 
    GROUP BY orderid) b ON a.orderid = b.orderid 
     LEFT JOIN 
    (SELECT 
     orderid, SUM(amount) AS Price 
    FROM 
     report 
    WHERE 
     amountdescription = 'Principal' 
    GROUP BY orderid) c ON b.orderid = c.orderid 
     LEFT JOIN 
    (SELECT 
     DeliveryLand, ExternalOrderId 
    FROM 
     orders) d ON c.orderid = d.ExternalOrderId 
ORDER BY Date DESC 

我不得不做LEFT JOIN上的最後一個表來獲取DeliveryLand,但不從報告表的每個項目已在訂單表中的一項。

當我做了一些計算後,我發現這個查詢不會返回所有的條目。 「typ」=「退款」應該有25個條目,但我只能得到20個條目。某些訂單可能包含其他訂單(由orderid標識)可能不具有的條目。

我做了另外一個查詢,總結費,運費和價格僅僅爲退款:

SELECT 
    SUM(ROUND(a.Fees, 2)) AS Fees, 
    SUM(ROUND(b.Shipping, 2)) AS Shipping, 
    SUM(ROUND(c.Price, 2)) AS Price 
FROM 
    (SELECT 
     orderid, SUM(amount) AS Fees 
    FROM 
     report 
    WHERE 
     amounttype = 'ItemFees' 
      AND transactiontype = 'Refund' 
    GROUP BY orderid) a 
     LEFT JOIN 
    (SELECT 
     orderid, SUM(amount) AS Shipping 
    FROM 
     report 
    WHERE 
     amountdescription = 'Shipping' 
      AND transactiontype = 'Refund' 
    GROUP BY orderid) b ON a.orderid = b.orderid 
     LEFT JOIN 
    (SELECT 
     orderid, SUM(amount) AS Price 
    FROM 
     report 
    WHERE 
     amountdescription = 'Principal' 
      AND transactiontype = 'Refund' 
    GROUP BY orderid) c ON b.orderid = c.orderid 

前兩個結果,費用和運輸成本,正確總結了(我得到了原始數據進行比較),但最後一個,價格是不正確的,它太多了。我想有一些數據被LEFT JOIN截斷,但我無法弄清楚爲什麼以及在哪裏,特別是當我總結「transactiontype」=「Order」的同一個colums時,這個查詢工作得很好。

我不知道爲什麼有一些數據被截斷或丟失。有人能幫我解決這兩個查詢中令我困惑的JOIN嗎?如果您需要更多信息,請詢問。

預先感謝您!

€dited查詢:

SELECT 
    posteddate AS Date, 
    transactiontype AS Typ, 
    report.orderid AS AZNr, 
    ROUND(SUM((amounttype = 'ItemFees') * amount), 
      2) AS Fees, 
    ROUND(SUM((amountdescription = 'Shipping') * amount), 
      2) AS Shipping, 
    ROUND(SUM((amountdescription = 'Principal') * amount), 
      2) AS Price, 
    orders.DeliveryLand, 
    articles.ItemVAT AS VAT 
FROM 
    report 
     LEFT JOIN 
    orders ON report.orderid = orders.ExternalOrderID 
     LEFT JOIN 
    articles ON report.sku = articles.ItemID 
GROUP BY report.orderid , transactiontype 

回答

0

我想通過簡化查詢開始,它可以在不JOIN s內寫入。我也會質疑爲什麼ROUND之前的SUM這將花費更長的時間並且不夠精確。

我會怎麼做:

SELECT orderid, 
     ROUND(SUM((amountdescription='ItemFees')*amount), 2)) AS Fees, 
     ROUND(SUM((amountdescription='Shipping')*amount), 2)) AS Shipping, 
     ROUND(SUM((amountdescription='Principal')*amount), 2)) AS Price 
    FROM report 
    WHERE transactiontype='Refund' 
GROUP BY orderid 

作爲解釋(amountdescription='ItemFees')返回1,如果真,否則爲0,因此金額合計只有指定的每個條件相匹配的..你可以使用,如果你的時間越長CASE喜歡:

SUM(CASE WHEN *condition* THEN amount ELSE 0 END) 

你可以從它依賴於連接了所有子表的原始查詢失去一些數據。如果沒有Fees那麼訂單的ShippingPrice會沒有被退回。

UPDATE

,如果你有相同的OrderID 2個transactiontypes不要忘了,他們都將算作相同的順序,它們都將被包含在同總和..分別獲得交易類型使用:

SELECT orderid, transactiontype, 
     ROUND(SUM((amountdescription='ItemFees')*amount), 2)) AS Fees, 
     ROUND(SUM((amountdescription='Shipping')*amount), 2)) AS Shipping, 
     ROUND(SUM((amountdescription='Principal')*amount), 2)) AS Price 
    FROM report 
GROUP BY orderid, transactiontype 
+0

我試過了你的查詢。通過選擇transactiontype ='Refund',我可以得到所需的25行。但是當我選擇所有行而沒有選擇特殊的事務類型時,我只有20行的退款,其餘的被截斷。你知道爲什麼嗎? – Ceriana 2014-09-30 19:50:37

+0

這應該是不可能的..你不能通過刪除WHERE來減少結果集。反而改變了WHERE?你的一些行是否返回NULL,但實際上是在那裏? – Arth 2014-09-30 19:54:49

+0

是的,有些行不包含Shipping。這就是爲什麼我嘗試了LEFT JOIN。這些缺少的字段會導致「0.00」,但在沒有事務類型的情況下選擇時,它們會被截斷。 – Ceriana 2014-09-30 19:59:56