2014-11-01 76 views
0

我有以下查詢:如何使用聚合函數?

SELECT dbo.saleDocumentDetails.ID, 
     dbo.saleDocumentDetails.DocumentID, 
     dbo.saleDocumentDetails.invProductID, 
     dbo.saleDocumentDetails.Qty, 
     dbo.saleDocumentDetails.QtyConfirmed, 
     dbo.saleDocumentDetails.AmountPrice, 
     dbo.saleDocumentDetails.AmountUserPrice, 
     dbo.saleDocumentDetails.AmountCost, 
     dbo.saleDocumentDetails.RespiteDays, 
     dbo.saleDocumentDetails.Date_UpdateLast, 
     CASE 
      WHEN io1.Code = '33' THEN SUM(idd.Qty) 
      ELSE SUM(0) 
     END        AS Qty_BackSale, 
     CASE 
      WHEN io1.Code = '41' THEN SUM(idd.Qty) 
      ELSE SUM(0) 
     END        AS Qty_Stock 
FROM dbo.saleDocuments 
     INNER JOIN dbo.saleDocumentDetails 
      ON dbo.saleDocuments.ID = dbo.saleDocumentDetails.DocumentID 
     LEFT OUTER JOIN dbo.invDocumentDetails AS idd 
      ON dbo.saleDocumentDetails.ID = idd.saleDocumentDetailID 
     LEFT OUTER JOIN dbo.invDocuments AS id2 
      ON id2.ID = idd.invDocumentID 
     LEFT JOIN dbo.invOperations  AS io1 
      ON (io1.ID = id2.invOperationID) 
WHERE (dbo.saleDocumentDetails.ID = 1295617) 
GROUP BY 
     dbo.saleDocumentDetails.ID, 
     dbo.saleDocumentDetails.DocumentID, 
     dbo.saleDocumentDetails.invProductID, 
     dbo.saleDocumentDetails.Qty, 
     dbo.saleDocumentDetails.QtyConfirmed, 
     dbo.saleDocumentDetails.AmountPrice, 
     dbo.saleDocumentDetails.AmountUserPrice, 
     dbo.saleDocumentDetails.AmountCost, 
     dbo.saleDocumentDetails.RespiteDays, 
     dbo.saleDocumentDetails.Date_UpdateLast, 
     io1.Code 

其結果是:

enter image description here

我想實現:

enter image description here

我應該如何更改查詢?

回答

1

如前所述JRLambert,移動SUM()CASE,並刪除io1.CodeGROUP BY

0

您是否試圖通過dbo.saleDocumentDetails.ID刪除組?

+1

他需要保持這一點。 – JRLambert 2014-11-01 06:49:49

+0

是的。但除了最後兩列以外,所有列都是相同的。 – breceivemail 2014-11-01 07:11:27

1

將在CASE報表SUM()CASE之外:

SELECT 
     dbo.saleDocumentDetails.ID, 
     dbo.saleDocumentDetails.DocumentID, 
     dbo.saleDocumentDetails.invProductID, 
     dbo.saleDocumentDetails.Qty, 
     dbo.saleDocumentDetails.QtyConfirmed, 
     dbo.saleDocumentDetails.AmountPrice, 
     dbo.saleDocumentDetails.AmountUserPrice, 
     dbo.saleDocumentDetails.AmountCost, 
     dbo.saleDocumentDetails.RespiteDays, 
     dbo.saleDocumentDetails.Date_UpdateLast, 
     SUM(CASE 
      WHEN io1.Code = '33' THEN idd.Qty 
      ELSE 0 
     END)        AS Qty_BackSale, 
     SUM(CASE 
      WHEN io1.Code = '41' THEN idd.Qty 
      ELSE 0 
     END)        AS Qty_Stock 
    FROM 
     dbo.saleDocuments 
     INNER JOIN dbo.saleDocumentDetails 
      ON dbo.saleDocuments.ID = dbo.saleDocumentDetails.DocumentID 
     LEFT OUTER JOIN dbo.invDocumentDetails AS idd 
      ON dbo.saleDocumentDetails.ID = idd.saleDocumentDetailID 
     LEFT OUTER JOIN dbo.invDocuments AS id2 
      ON id2.ID = idd.invDocumentID 
     LEFT JOIN dbo.invOperations  AS io1 
      ON (io1.ID = id2.invOperationID) 
WHERE 
    (dbo.saleDocumentDetails.ID = 1295617) 
GROUP BY 
    dbo.saleDocumentDetails.ID, 
    dbo.saleDocumentDetails.DocumentID, 
    dbo.saleDocumentDetails.invProductID, 
    dbo.saleDocumentDetails.Qty, 
    dbo.saleDocumentDetails.QtyConfirmed, 
    dbo.saleDocumentDetails.AmountPrice, 
    dbo.saleDocumentDetails.AmountUserPrice, 
    dbo.saleDocumentDetails.AmountCost, 
    dbo.saleDocumentDetails.RespiteDays, 
    dbo.saleDocumentDetails.Date_UpdateLast, 
    io1.Code; 
+0

謝謝,需要從組中刪除'io1.Code'。 – breceivemail 2014-11-01 07:15:02