MYSQL合并,计数和分隔ID相同但值不同的行实例

时间:2018-11-15 09:04:13

标签: mysql

目前,我有一个查询类似

Start

,查询写成这样:

SELECT username, exerciseId, frustrated, bored 
FROM affecttutor.selfreportfrustration 
order by username;

我正在尝试使其看起来像这样

Finish

可能会做些什么?谢谢您的宝贵时间!

1 个答案:

答案 0 :(得分:1)

您可以尝试使用条件聚合

select 
   username,exerciseid,
   count(case when frustrated='Y' then 1 end) as frustrated,
   count(case when frustrated<>'Y' then 1 end) as notfrustrated,
   count(case when bored<>'N' then 1 end) as bored ,
   count(case when bored='N' then 1 end) as notbored
from affecttutor.selfreportfrustration
group by username,exerciseid