【发布时间】:2014-03-19 11:38:51
【问题描述】:
在这里我有查询我的报告并成功执行,但我没有正确得到答案,在我的答案中,我想要收据、MRN_P、问题、拒绝、Transfer_P 和 Transfer_M 的总数 但我没有得到图像中显示的空值....请帮助我解决我的代码不正确的地方
ALTER PROCEDURE [dbo].[DailyProcessReport]
AS
BEGIN
SELECT
tra_item,
IsNull(sum([Receipt]),0)as Receipt,
IsNull(sum([MRN_P]), 0)as MRN_P,
IsNull(sum([Issue]), 0)as Issue,
IsNull(sum([Rejection]), 0) as Rejection,
IsNull(sum([Transfer_P]),0)as Transfer_P,
IsNull(sum([Transfer_M]),0)as Transfer_M,
[Receipt] + [MRN_P] + [Issue] + [Rejection] + [Transfer_P] + [Transfer_M] as 'Total'
FROM
(
SELECT tra_item, tra_quantity, tra_type
FROM
tra_master
) as pvt
PIVOT (Sum(tra_quantity) FOR tra_type IN ([Receipt], [MRN_P], [Issue], [Rejection], [Transfer_P], [Transfer_M])) as pvt
Group by tra_item, [Receipt], [MRN_P], [Issue], [Rejection], [Transfer_P], [Transfer_M]
END
结果我们得到在总列中我想要所有行的总和而不是 Null
【问题讨论】:
-
应该不需要
GROUP BY,也不需要外部SELECT子句中的第二组SUM()s,因为PIVOT已经执行了必要的SUM()-为什么你有那些?
标签: c# stored-procedures sql-server-2008-r2 pivot-table