1

如何对当前查询求和或添加 (PDetail.DETAIL_FOR_QTY)?

SELECT        PDetail.PLU, PDetail.DETAIL_FOR_QTY, PLU.PLU_DESC, PLU.LAST_PRICE
FROM            PDetail INNER JOIN
                     PLU ON PDetail.PLU = PLU.PLU_NUM
WHERE        (PDetail.DEPT = 26) AND (PDetail.StoreNumber IN (1, 2, 3, 4, 7, 8, 10, 12, 14, 16))    AND (PDetail.TIME_STAMP BETWEEN CONVERT(DATETIME, 
                     '2013-06-20 00:00:00', 102) AND CONVERT(DATETIME, '2013-06-20 23:59:59', 102))
ORDER BY PLU.PLU_DESC

目前,我得到这样的东西:

08024401    1   item1   17.4900
08048003    1   item2   22.9900
08048004    1   item3   22.9900
08048004    1   item3   22.9900

我试图将这两个相加,因为它们是相同的(基于 PDetail INNER JOIN PLU ON PDetail.PLU = PLU.PLU_NUM):

PDetail.PLU    PDetail.DETAIL_FOR_QTY      PLU.PLU_DESC            Don't need to add this
08048004        1                                 item3                       22.9900
08048004        1                                 item3                       22.9900

期望的结果:

08024401    1   item1   17.4900
08048003    1   item2   22.9900
08048004    2   item3   22.9900
4

3 回答 3

2

根据对问题的评论,您只需要

select PDetail.PLU, 
       sum(DETAIL_FOR_QTRY) as [QUANTITY SUM],
       PLU.PLU_Description,
       MAX(PLU.Last_Price)
FROM   PDetail 
INNER JOIN PLU 
ON PDetail.PLU = PLU.PLU_NUM
WHERE    (PDetail.DEPT = 26) 
AND      (PDetail.StoreNumber IN (1, 2, 3, 4, 7, 8, 10, 12, 14, 16))
AND      (PDetail.TIME_STAMP BETWEEN 
                                CONVERT(DATETIME,'2013-06-20 00:00:00', 102) 
                            AND CONVERT(DATETIME, '2013-06-20 23:59:59', 102))
GROUP BY PDetail.PLU, PLU.PLU_Description    
于 2013-06-21T19:34:42.937 回答
0

你可以这样做。总和(PDetail.DETAIL_FOR_QTY)。然后将 Group By 用于非唯一列。

于 2013-06-21T19:24:35.953 回答
0

这成功了:

SELECT        PDetail.PLU, SUM(PDetail.DETAIL_FOR_QTY) AS DETAIL_FOR_QTY, PLU.PLU_DESC,       PLU.LAST_PRICE
FROM            PDetail INNER JOIN
                     PLU ON PDetail.PLU = PLU.PLU_NUM
WHERE        (PDetail.DEPT = 26) AND (PDetail.StoreNumber IN (1, 2, 3, 4, 7, 8, 10, 12, 14, 16)) AND (PDetail.TIME_STAMP BETWEEN CONVERT(DATETIME, 
                     '2013-06-20 00:00:00', 102) AND CONVERT(DATETIME, '2013-06-20 23:59:59', 102))
GROUP BY PDetail.PLU, PLU.PLU_DESC, PLU.LAST_PRICE, PDetail.DETAIL_FOR_QTY
ORDER BY PLU.PLU_DESC, PDetail.PLU
于 2013-06-21T19:59:48.950 回答