这是我的T-SQL代码:
SELECT H.ORDER_DATE 'Order Date',
SUM(D.WORK_QTY) 'Work Qty',
SUM(TD. TRAN_QTY) 'Transaction Qty',
IM.PUMJ_CD
FROM [dbo].[ORDER_HEADER] H
LEFT JOIN ORDER_DETAIL D ON D.ORDER_ID = H.ORDER_ID
LEFT JOIN TRAN_HEADER T ON T.ORDER_ID = H.ORDER_ID
LEFT JOIN TRAN_DETAIL TD ON TD.TRAN_ID = T.TRAN_ID
LEFT JOIN TRAN_TYPE TT ON TT.TRAN_TYPE_ID = TD.TRAN_TYPE_ID
LEFT JOIN ITM_MST IM ON IM.ITM_NM = D.ITEM_NAME
WHERE TD.TRAN_TYPE_ID = '231'
AND PUMJ_CD IN ('BF','BR','SHFT','BJ','TJ')
AND H.ORDER_DATE BETWEEN '20170403' AND '20170407'
GROUP BY IM.PUMJ_CD,
H.ORDER_DATE
ORDER BY H.ORDER_DATE
并返回记录集:
Order Date | Work Qty | Transaction Qty | PUMJ_CD
-------------------------------------------------
20170403 | 10 | 7 | BF
20170403 | 24 | 12 | BR
20170403 | 16 | 14 | TJ
20170404 | 15 | 10 | BF
是否可以对记录集中的行进行求和,其中: '订单日期'是一样的,而且 PUMJ_CD IN(' BF',' BR') 并将结果记录集看起来像:
Order Date | Work Qty | Transaction Qty | PUMJ_CD
-------------------------------------------------
20170403 | 34 | 19 | TOTAL
20170403 | 16 | 14 | TJ
20170404 | 15 | 10 | BF
我一直试图在一个查询中找到一种方法来做到这一点,但由于我的知识有限,我还没有成功。我尝试过在线查看,但我发现很难用google中的问题描述我需要的东西。如果有人能帮忙或指出我正确的方向,我们将非常感激。
答案 0 :(得分:1)
是。您可以使用CASE
进行汇总,也可以使用SELECT
:
SELECT H.ORDER_DATE as [Order Date],
SUM(D.WORK_QTY) as [Work Qty],
SUM(TD. TRAN_QTY) as [Transaction Qty],
(CASE WHEN IM.PUMJ_CD IN ('BF','BR') THEN 'TOTAL' ELSE IM.PUMJ_CD
END) as PUMJ_CD
FROM . . .
GROUP BY H.ORDER_DATE,
(CASE WHEN IM.PUMJ_CD IN ('BF','BR') THEN 'TOTAL' ELSE IM.PUMJ_CD
END)
这与你想要的有点不同。对于这两个值,它将始终返回TOTAL
。
如果您想要个人价值,可以稍作修改:
SELECT H.ORDER_DATE as [Order Date],
SUM(D.WORK_QTY) as [Work Qty],
SUM(TD. TRAN_QTY) as [Transaction Qty],
(CASE WHEN COUNT(*) = 1 THEN MIN(IM.PUMJ_CD)
ELSE (CASE WHEN IM.PUMJ_CD IN ('BF','BR') THEN 'TOTAL' ELSE IM.PUMJ_CD)
END) as PUMJ_CD
FROM . . .
GROUP BY H.ORDER_DATE,
(CASE WHEN IM.PUMJ_CD IN ('BF','BR') THEN 'TOTAL' ELSE IM.PUMJ_CD
END)
答案 1 :(得分:0)
在两个数据集上使用Union。
1 - 没有BR和BF
2 - 仅BR和BF为总计
SELECT H.ORDER_DATE 'Order Date',
SUM(D.WORK_QTY) 'Work Qty',
SUM(TD. TRAN_QTY) 'Transaction Qty',
IM.PUMJ_CD
FROM [dbo].[ORDER_HEADER] H
LEFT JOIN ORDER_DETAIL D ON D.ORDER_ID = H.ORDER_ID
LEFT JOIN TRAN_HEADER T ON T.ORDER_ID = H.ORDER_ID
LEFT JOIN TRAN_DETAIL TD ON TD.TRAN_ID = T.TRAN_ID
LEFT JOIN TRAN_TYPE TT ON TT.TRAN_TYPE_ID = TD.TRAN_TYPE_ID
LEFT JOIN ITM_MST IM ON IM.ITM_NM = D.ITEM_NAME
WHERE TD.TRAN_TYPE_ID = '231'
AND PUMJ_CD IN ('SHFT','BJ','TJ') -- NO BR BF
AND H.ORDER_DATE BETWEEN '20170403' AND '20170407'
GROUP BY IM.PUMJ_CD,
H.ORDER_DATE
union
SELECT H.ORDER_DATE,
SUM(D.WORK_QTY),
SUM(TD. TRAN_QTY),
'TOTAL'
FROM [dbo].[ORDER_HEADER] H
LEFT JOIN ORDER_DETAIL D ON D.ORDER_ID = H.ORDER_ID
LEFT JOIN TRAN_HEADER T ON T.ORDER_ID = H.ORDER_ID
LEFT JOIN TRAN_DETAIL TD ON TD.TRAN_ID = T.TRAN_ID
LEFT JOIN TRAN_TYPE TT ON TT.TRAN_TYPE_ID = TD.TRAN_TYPE_ID
LEFT JOIN ITM_MST IM ON IM.ITM_NM = D.ITEM_NAME
WHERE TD.TRAN_TYPE_ID = '231'
AND PUMJ_CD IN ('BF','BR') -- ONLY BF AND BR AS TOTAL
AND H.ORDER_DATE BETWEEN '20170403' AND '20170407'
GROUP BY H.ORDER_DATE