我有一个简单的数据库(使用MySql),我需要生成一个报告。 我有一个schedule_plan表,由用户提交。
和我的表userinfo包含name,age dll
所以我需要一个报告,它将日期显示为列以及userinfo表中的名称。
Report
+------+--------+--------+--------+-------+
| Name | 1 | 2 | 3 | until day 30/31 |
+------+--------+--------+--------+-------+
| Bob | ON | ON | ON | ... |
| Joe | OFF | ON | OFF | ... |
| Jim | ON | ON | ON | ... |
如果schedule_plan中的absence_code为null,则为ON,如果不为null OFF
答案 0 :(得分:0)
create table attendance
(user_id int,attendance_code varchar(1),dte date)
*/
truncate table attendance;
insert into attendance values
(1,'a','2016-01-01'),
(1,null,'2016-01-02'),
(1,'a','2016-01-03'),
(1,'a','2016-01-04'),
(1,'a','2016-01-05'),
(2,'a','2016-01-01'),
(2,'a','2016-01-02'),
(2,null,'2016-01-03'),
(2,null,'2016-01-04'),
(2,'a','2016-01-05')
;
select a.user_id,
max(case when day(a.dte) = 1 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day1,
max(case when day(a.dte) = 2 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day2,
max(case when day(a.dte) = 3 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day3,
max(case when day(a.dte) = 4 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day4,
max(case when day(a.dte) = 5 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day5
from attendance a
group by a.user_id;