Access转换为T-SQL

时间:2013-04-14 22:32:15

标签: sql sql-server-2008 ms-access pivot transform

我有这个我正在转换为T-SQL的Access查询:

TRANSFORM Sum(dbo.vw_SIMP_EN_F_Tx_AccRec_8.Outstanding) AS SumOfOutstanding
SELECT dbo.vw_SIMP_EN_F_Tx_AccRec_88.To_ICJ, q_F_TX_AccRec_8.From_Reference__c, 
       q_F_TX_AccRec_8.[From Account]
FROM dbo.vw_SIMP_EN_F_Tx_AccRec_8
GROUP BY dbo.vw_SIMP_EN_F_Tx_AccRec_8.To_ICJ, 
         dbo.vw_SIMP_EN_F_Tx_AccRec_8.From_Reference__c, 
         dbo.vw_SIMP_EN_F_Tx_AccRec_8.[From Account]
PIVOT dbo.vw_SIMP_EN_F_Tx_AccRec_8.Age 
  In ("Current","30 days","60 days","90 days");

到目前为止,我已经在T-SQL中完成了这项工作:

SELECT [To_ICJ],
   [From_Reference__c],
   [From Account],
   [Current],
   [30 days],
   [60 days],
   [90 days]
FROM
(Select Sum(dbo.vw_SIMP_EN_F_Tx_AccRec_8.[Outstanding]) AS [SumOfOutstanding],
    dbo.vw_SIMP_EN_F_Tx_AccRec_8.Age AS [PIVOT_ITEM]
FROM dbo.vw_SIMP_EN_F_Tx_AccRec_8
GROUP BY [To_ICJ],
     [From_Reference__c],
     [From Account],
     Age) AS p PIVOT(
     Sum([SumOfOutstanding]) FOR [PIVOT_ITEM]
 In ([Current],[30 days],[60 days],[90 days])) as pvt

但是我的T-SQL导致了这些错误:

  

Msg 207,Level 16,State 1,Line 1
  列名称“To_ICJ”无效。

     

Msg 207,Level 16,State 1,Line 2
  列名称“From_Reference__c”无效。

     

Msg 207,Level 16,State 1,Line 3
  列名称“来自帐户”无效。

知道我做错了吗?

1 个答案:

答案 0 :(得分:2)

您的子查询中缺少3列选择,请尝试此

SELECT [To_ICJ],
   [From_Reference__c],
   [From Account],
   [Current],
   [30 days],
   [60 days],
   [90 days]
FROM
(Select 
     A.[To_ICJ],
     A.[From_Reference__c],
     A.[From Account],
    Sum(A.[Outstanding]) AS [SumOfOutstanding],
    A.Age AS [PIVOT_ITEM]
FROM dbo.vw_SIMP_EN_F_Tx_AccRec_8 A
GROUP BY A.[To_ICJ],
     A.[From_Reference__c],
     A.[From Account],
     A.Age
) AS p PIVOT(
     Sum([SumOfOutstanding]) FOR [PIVOT_ITEM]
 In ([Current],[30 days],[60 days],[90 days])) as pvt