我正在尝试将MS Access Cross Tab查询转换为SQL Server存储过程,但在SQL Server中转换数据存在问题。
这是我要转换的MS Access交叉查询 -
TRANSFORM Sum(NZ(Actuals!Amount,0)) AS Amount
SELECT Actuals.PS_OV, Actuals.Period, Actuals.Program, Actuals.Actuals_Year
FROM Actuals
GROUP BY Actuals.PS_OV, Actuals.Period, Actuals.Program, Actuals.Actuals_Year
PIVOT Actuals.Source;
Source
列中的值(即Equipment, Expense, Furniture, Leasehold
)会转到列。请建议如何在SQL Server存储过程中执行此操作?
非常感谢。
答案 0 :(得分:2)
在SQL Server中,PIVOT
就像这样:
SELECT *
FROM
(
SELECT PS_OV
, Period
, Program
, Actuals_Year
, IsNull(Amount, 0) as Amount
, Source
FROM Actuals
) x
PIVOT
(
Sum(Amount)
FOR Source IN ([Equipment], [Expense], [Furniture], [Leasehold])
) p