2016-06-22 2 views
0

보고서를 생성해야하는 간단한 데이터베이스 (MySql 사용)가 있습니다. 사용자가 제출 한 schedule_plan 테이블이 있습니다.보고서를 만들 행과 열 만들기

this my column in my table schedule_plan

와 이름을 포함하는 사용자 정보 내 테이블, 나이 DLL

그래서 나는 사용자 정보 테이블에서 컬럼과 일뿐만 아니라 이름을 표시하는 보고서가 필요합니다.

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가 OFF NULL이 아닌 경우 다음 ON null의 경우

+0

유 시도 했는가? ........이를 참조하십시오 http://stackoverflow.com/questions/ 14834290/mysql-query-to-dynamically-rows-to-columns로 변환하고 쿼리를 업데이트했는데 어느 쿼리를 시도했는지 ... –

+0

내 쿼리는 MAX (IF (부재 _ 코드가 null, 'ON', 'OFF')) 1 일 MAX func 나는 나를 쓸모 없어한다고 생각합니다. –

답변

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; 
+0

그래서 나는 31 일까지 하루 (a.dte)를 써야합니다. 단지 28 또는 30 일 경우 어떻습니까? 나는 숫자가 –

+0

또는 월 29 일 또는 월말 인 것을 알 수 있습니다. 2 월 29 일부터 31 일까지 부적절한 행동을하는 것이 용납되지 않으면 SQL 문장을 작성하고 제공된 솔루션을 기반으로 동적 SQL을 사용하여 실행해야합니다. –