WITH ActiveAccountCodes AS
(
SELECT BWfkYear, BWYearID, BWFund, BWMajorObject, BWObject, BWObjectDescr, BWAmount, BWDescr FROM @TmpProposedAccountCodes
UNION
SELECT BWfkYear, BWYearID, BWFund, BWMajorObject, BWObject, BWObjectDescr, BWAmount, BWDescr FROM @TmpCurrentAccountCodes
UNION
SELECT BWfkYear, BWYearID, BWFund, BWMajorObject, BWObject, BWObjectDescr, BWAmount, BWDescr FROM @TmpBudget1AccountCodes
UNION
SELECT BWfkYear, BWYearID, BWFund, BWMajorObject, BWObject, BWObjectDescr, BWAmount, BWDescr FROM @TmpBudget2AccountCodes
UNION
SELECT BWfkYear, BWYearID, BWFund, BWMajorObject, BWObject, BWObjectDescr, BWAmount, BWDescr FROM @TmpBudget3AccountCodes
)
SELECT BWObject, BWObjectDescr, Proposed, Current, Actual1, Actual2, Actual3, BWDescr
FROM ActiveAccountCodes
PIVOT(SUM(BWAmount) FOR BWYearID IN ([SELECT BWYearID FROM @TmpProposedAccountCodes AS [Proposed]], [SELECT BWYearID FROM @TmpCurrentAccountCodes AS [Current]],
[SELECT BWYearID FROM @TmpBudget1AccountCodes AS Actual1],
[SELECT BWYearID FROM @TmpBudget2AccountCodes AS Actual2],
[SELECT BWYearID FROM @TmpBudget3AccountCodes AS Actual3] )) AS PVT
如上所示,我试图使用CTE来执行PIVOT表达式。当我尝试选择我在PIVOT表达式中计算和命名的两个列(Proposed和Current)时,我在SSMS中得到了红线。
具体来说,这些行给我的问题是:
SELECT BWObject, BWObjectDescr, Proposed, Current, Actual1, Actual2, Actual3, BWDescr
FROM ActiveAccountCodes
PIVOT(SUM(BWAmount) FOR BWYearID IN ([SELECT BWYearID FROM @TmpProposedAccountCodes AS [Proposed]], [SELECT BWYearID FROM @TmpCurrentAccountCodes AS [Current]],
[SELECT BWYearID FROM @TmpBudget1AccountCodes AS Actual1],
[SELECT BWYearID FROM @TmpBudget2AccountCodes AS Actual2],
[SELECT BWYearID FROM @TmpBudget3AccountCodes AS Actual3] )) AS PVT
我在查询的PIVOT部分中定义列名时没有收到错误,但是当我尝试在上面使用它们时,我收到错误。我不确定这个的原因。谢谢你的帮助。在发布问题之前,我确实做了一些研究。
答案 0 :(得分:0)
除非你想尝试,否则不要认为你需要使用CTE来做你正在做的事。
我相信下面没有CTE的例子就是你要找的。如果您正在寻找CTE的例子,请发表评论。
declare @TmpProposedAccountCodes table (BWfkYear int, BWYearID int, BWFund int, BWObjectDescr varchar(10), BWAmount money, BWDescr varchar(10))
declare @TmpCurrentAccountCodes table (BWfkYear int, BWYearID int, BWFund int, BWObjectDescr varchar(10), BWAmount money, BWDescr varchar(10))
declare @TmpBudget1AccountCodes table (BWfkYear int, BWYearID int, BWFund int, BWObjectDescr varchar(10), BWAmount money, BWDescr varchar(10))
declare @TmpBudget2AccountCodes table (BWfkYear int, BWYearID int, BWFund int, BWObjectDescr varchar(10), BWAmount money, BWDescr varchar(10))
declare @TmpBudget3AccountCodes table (BWfkYear int, BWYearID int, BWFund int, BWObjectDescr varchar(10), BWAmount money, BWDescr varchar(10))
insert into @TmpProposedAccountCodes
select 0,0,0,'011', 0.1, '0.1'
insert into @TmpBudget1AccountCodes
select 2,2,2,'211', 2.1, '2.1'
insert into @TmpBudget2AccountCodes
select 3,3,3,'311', 3.1, '3.1'
insert into @TmpBudget3AccountCodes
select 4,4,4,'411', 4.1, '4.1'
insert into @TmpCurrentAccountCodes
select 1,1,1,'111', 1.1, '1.1'
select *
from (
SELECT BWfkYear, 'Proposed' BWYearID, BWFund, BWObjectDescr, BWAmount, BWDescr FROM @TmpProposedAccountCodes
UNION
SELECT BWfkYear, 'Current' BWYearID, BWFund, BWObjectDescr, BWAmount, BWDescr FROM @TmpCurrentAccountCodes
UNION
SELECT BWfkYear, 'Actual1' BWYearID, BWFund, BWObjectDescr, BWAmount, BWDescr FROM @TmpBudget1AccountCodes
UNION
SELECT BWfkYear, 'Actual2' BWYearID, BWFund, BWObjectDescr, BWAmount, BWDescr FROM @TmpBudget2AccountCodes
UNION
SELECT BWfkYear, 'Actual3' BWYearID, BWFund, BWObjectDescr, BWAmount, BWDescr FROM @TmpBudget3AccountCodes
) x
PIVOT(SUM(BWAmount) FOR BWYearID IN (
[Proposed],
[Current],
[Actual1],
[Actual2],
[Actual3] )) AS PVT