我正在尝试在我的工作台中打开一个脚本,但关键字PIVOT用红色加下划线,并显示错误消息:`语法错误,意外IDENT_QUOTED。 脚本:
select * from
(select c1.id, a.num0,a.num1 from table1 c1
Inner Join
(select c2.id, if(team=1,1,0) as num0, if(team=2,1,0)as num1 from table1 c2,table2 r2
where c2.q_id = 2046 and r2.q_id = 2046 group by c2.d)a on a.id = c1.id) pvt
PIVOT(
For content
IN([team1],[team2]))pvt2
答案 0 :(得分:0)
答案 1 :(得分:0)
MySQL没有PIVOT功能。但是,可以使用具有CASE
表达式的聚合函数来复制此功能。
根据您的原始查询,您似乎需要类似的内容:
select t2.id,
sum(case when team = 1 then 1 else 0 end) num0,
sum(case when team = 2 then 1 else 0 end) num1
from table1 t1
inner join table2 t2
on t1.q_id = t2.q_id
group by t2.id