将MS Access Crosstab查询转换为SQL Server存储过程

时间:2012-07-25 23:50:50

标签: sql-server ms-access pivot

我正在尝试将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存储过程中执行此操作?

非常感谢。

1 个答案:

答案 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