2016-06-22 10 views
0

レポートを生成するために必要な単純なデータベース(MySqlを使用)があります。 私は、ユーザーによって送信されたschedule_planというテーブルを持っています。レポートを作成するために行を作成する

this my column in my table schedule_plan

と私の名前を含む表のuserinfo、年齢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がOFFヌルでないならば、ON nullの場合

+0

uはしてみてくださいでしたか?........これを参照してくださいhttp://stackoverflow.com/questions/ 14834290/mysql-query-to-dynamic-rows-to-columnsを変換してクエリを更新するクエリもあります... –

+0

クエリが必要なので、私はうまくいきません。 MAX(IF(abs_code is 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

です。 2月の29-31のような不適切なものが受け入れられない場合は、SQL文を作成し、提供したソリューションに基づいて動的SQLを使用して実行する必要があります。 –

関連する問題