我认为这很简单,但我很挣扎(可能是星期五的大脑)
我有一张桌子:
Staff department job_date job Hours --------------------------------------------------------------- Chris Software 2011-03-02 Writing Code 3 Chris Software 2011-03-04 Holiday 7 Bob QA 2011-03-02 Testing 4 Frank Design 2011-03-11 Meeting 1
现在,我需要一个查询,根据工作字段中的最新情况,按天“分组”结果,即..所以我得到每个员工在工作和假期时花费的小时数:
结果集:
Staff Worked Hours Holiday Hours Department --------------------------------------------------------------- Chris 3 7 Software Bob 4 0 QA Frank 1 0 Design --------------------------------------------------------------- Total 8 7
感谢您提供任何帮助!
克里斯
答案 0 :(得分:1)
SELECT department, staff,
SUM(IF(job = 'Holiday', 0, hours)) AS work_hours,
SUM(IF(job = 'Holiday', hours, 0)) AS holiday_hours
FROM mytable
GROUP BY
department, staff WITH ROLLUP
答案 1 :(得分:0)
select staff,
sum(case when job='holiday' then 0 else hours end) `Worked Hours`,
sum(case when job='holiday' then hours else 0 end) `Holiday Hours`,
department
from tbl
group by staff, department