这是我的案例表
id empid reaction date_t
1 emp109 interested 2017-09-12
2 emp109 not_interested 2017-09-13
3 emp109 maybe 2017-09-13
我想要实现的是特定员工ID
date_t | number of interested |number of not interested | number of maybe
就像2017-09-12和2017-09-13的员工ID emp109一样,表格看起来像这样
date_t | number of interested |number of not interested | maybe
2017-09-12 1 0 0
2017-09-13 0 1 1
这是我到目前为止所尝试的
SELECT * FROM
(SELECT cases.date_t, cases.empid,COUNT(cases.reaction) as interested FROM `cases` WHERE cases.reaction="interested" and cases.empid="EMP12654" GROUP by cases.date_t)alpha
JOIN
(SELECT cases.date_t, cases.empid,COUNT(cases.reaction) as not_interested FROM `cases` WHERE cases.reaction="not_interested" and cases.empid="EMP12654" GROUP by cases.date_t)beta
on alpha.date_t=beta.date_t
JOIN
(SELECT cases.date_t, cases.empid,COUNT(cases.reaction) as maybe FROM `cases` WHERE cases.reaction="maybe" and cases.empid="EMP12654" GROUP by cases.date_t)zeta
on alpha.date_t=zeta.date_t
这不是给我想要的结果任何想法如何实现它?
答案 0 :(得分:0)
这称为旋转。
您可以使用条件聚合在MySQL中执行此操作:
select date_t,
sum(reaction = 'interested') as interested,
sum(reaction = 'not_interested') as not_interested,
sum(reaction = 'maybe') as maybe
from your_table
group by date_t;
以上使用的事实是MySQL将true视为1,将false视为0。