我使用以下问题,但总和答案不正确。
SELECT DISTINCT TOP 3
TB_GOODS_RECEIPT_HEAD.DOC_NO AS PDN,
DOC_NO,
CONVERT(varchar(8), TB_GOODS_RECEIPT_HEAD.DOC_DT, 112) AS DATE_1,
CONVERT(varchar(8), TB_GOODS_RECEIPT_HEAD.ACT_RECEIPT_DATE, 112) AS DATE_2,
SUM(TB_GOODS_RECEIPT_LINE.QTY) AS RCVQTY,
SUM TB_GOODS_RECEIPT_LINE.ADVICED_QTY) AS ADVQTY
FROM TB_GOODS_RECEIPT_HEAD
INNER JOIN TB_GOODS_RECEIPT_LINE ON TB_GOODS_RECEIPT_HEAD.RECEIPT_HEAD_ID =
TB_GOODS_RECEIPT_LINE.RECEIPT_HEAD_ID
AND TB_GOODS_RECEIPT_LINE.X_PART_ID = (
SELECT X_PART_ID FROM TB_X_PART WHERE PARTNO_BUYER = '146620'
AND CAT_COMPANY_ID = '30')
GROUP BY DOC_NO, DOC_DT,ACT_RECEIPT_DATE,ADVICED_QTY
ORDER BY DATE_2 DESC, DATE_1 DESC
输出给了我
PDN DOC_NO DATE_1 DATE_2 RCVQTY ADVQTY
1 125527 125527 20190312 20190313 50.000 50.000
2 124335 124335 20190108 20190109 4.000 4.000
3 124335 124335 20190108 20190109 50.000 50.000
第2行和第3行只能是数量为54.000的一行,因为PDN,DOC_NO,DATE_1和DATE_2上的值相同。 我迷路了。
/ Stefan
答案 0 :(得分:0)
您可以尝试:
SELECT PDN,DOC_NO,DATE_1,DATE_2,SUM(RCVQTY),SUM(ADVQTY) 来自( 选择排名前三 TB_GOODS_RECEIPT_HEAD.DOC_NO AS PDN, DOC_NO, CONVERT(varchar(8),TB_GOODS_RECEIPT_HEAD.DOC_DT,112)AS DATE_1, CONVERT(varchar(8),TB_GOODS_RECEIPT_HEAD.ACT_RECEIPT_DATE,112)AS DATE_2, SUM(TB_GOODS_RECEIPT_LINE.QTY)作为RCVQTY, SUM TB_GOODS_RECEIPT_LINE.ADVICED_QTY)作为ADVQTY 来自TB_GOODS_RECEIPT_HEAD 内联TB_GOODS_RECEIPT_HEAD上的TB_GOODS_RECEIPT_LINE.RECEIPT_HEAD_ID = TB_GOODS_RECEIPT_LINE.RECEIPT_HEAD_ID AND TB_GOODS_RECEIPT_LINE.X_PART_ID =( 从TB_X_PART WHERE PARTNO_BUYER ='146620'中选择X_PART_ID AND CAT_COMPANY_ID ='30') GROUP BY DOC_NO,DOC_DT,ACT_RECEIPT_DATE,ADVICED_QTY ORDER BY DATE_2 DESC,DATE_1 DESC) GROUP BY DN,DOC_NO,DATE_1,DATE_2
答案 1 :(得分:0)
您不应按ADVICED_QTY分组。我认为这就是为什么在最后两行中没有得到一行而不是两行的原因。