sql server pivot语法

时间:2014-01-17 07:29:39

标签: sql sql-server pivot

我需要进行行列转置并尝试以下查询

select txn_date,
case when remarks is NULL then 'bank' else remarks  end as remarks
 from nibl
  PIVOT
(
count(txn_date)
FOR remarks IN (bank, remit)
) as pivot

上面的查询给出语法错误,如下所示

Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'pivot'.

2 个答案:

答案 0 :(得分:2)

pivot是SQL Server中的保留字,因此) as pivot在此处失败。

使用其他别名) as p

答案 1 :(得分:0)

你是否以这种方式尝试过......

select * from
(select txn_date,
case when remarks is NULL then 'bank' else remarks  end as remarks
 from nibl
 ) srs
  PIVOT
(
count(txn_date)
FOR remarks IN (bank, remit)
) as pivot