我有以下查詢:如何使用聚合函數?
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
其結果是:
我想實現:
我應該如何更改查詢?
他需要保持這一點。 – JRLambert 2014-11-01 06:49:49
是的。但除了最後兩列以外,所有列都是相同的。 – breceivemail 2014-11-01 07:11:27