当我这样做时:
select col1,case when [pivot1]=1 then '-' else '' end [pivot1],
case when [pivot2]=1 then '-' else '' end [pivot2]
from
(select col1,col2,col3 from tbl) as c
pivot
(sum(col3) for col2 in
([pivot1],[pivot2]))as pvt
一切正常。
当我这样做时:
select col1,[pivot1],[pivot2]
from
(select col1,col2,col3 from tbl) as c
pivot
(sum(case col3 when '-' then 1 else 0 end) for col2 in
([pivot1],[pivot2]))as pvt
我收到以下错误:
"Msg 156, Level 15, State 1, Line 31
Incorrect syntax near the keyword 'case'."
我的目的是为此转换编写单个case语句而不是多个case语句。
我做错了什么?
答案 0 :(得分:5)
用
替换你的case语句case when col3 = '-' then 1 else 0 end