How I can group the results by day in this query with SQL Server? -
i have table structure diary table:
create table diary ( [iddiary] bigint, [userid] int, [idday] numeric(18,0), [isanextrahour] bit ); insert diary ([iddiary], [userid], [idday], [isanextrahour]) values (51, 1409, 1, 0), (52, 1409, 1, 1), (53, 1409, 3, 0), (54, 1409, 5, 0), (55, 1409, 5, 1), (56, 1408, 2, 0);
and structure diarytimetable table:
create table diarytimetable ( [iddiary] bigint, [hour] varchar(50) ); insert diarytimetable ([iddiary], [hour]) values (51, '09:00'), (51, '09:30'), (51, '10:00'), (51, '10:30'), (51, '11:00'), (52, '15:00'), (52, '15:30'), (52, '16:00'), (52, '16:30'), (52, '17:00'), (53, '11:00'), (53, '11:30'), (53, '12:00'), (53, '12:30'), (53, '13:00'), (54, '10:00'), (54, '10:30'), (54, '11:00'), (54, '11:30'), (54, '12:00'), (55, '16:00'), (55, '16:30'), (55, '17:00'), (55, '17:30'), (55, '18:00'), (56, '15:00'), (56, '15:30'), (56, '16:00'), (56, '16:30'), (56, '17:00');
i used query max hour , min hour userid 1409, each day time thats enter , time thats leave work. idday correspond number of day of week. example 1 monday, 2 tuesday etc...
select d.iddiary, d.idday, min(hour) 'start time', max(hour) 'end time', isanextrahour diary d left join diarytimetable dt on d.iddiary = dt.iddiary userid = 1409 group d.iddiary, d.idday, isanextrahour
this query give result:
i want result:
day start time end time start time end time ----- ---------- -------- --------------- --------------- monday 09:00 11:00 15:00 17:00 wednessday 11:00 13:00 friday 10:00 12:00 16:00 18:00
i have column (isanextrahour) column indicates if row has hours in day, example employe start work in monday @ 09:00 11:00 , works again in afternoon @ 15:00 17:00, want know how can group hours in same row, hope i've been able express clearly, accept suggestions thanks.
select d.idday, min(case when isanextrahour = 0 hour end) 'start time', max(case when isanextrahour = 0 hour end) 'end time', min(case when isanextrahour = 1 hour end) 'start time', max(case when isanextrahour = 1 hour end) 'end time' diary d left join diarytimetable dt on dt.iddiary = d.iddiary userid = 1409 group d.idday
Comments
Post a Comment