2012-09-25 4 views
0

시간별로 10 분 전과 10 분 후에 발생하는 항목의 수를 계산해야합니다. 항목을 개별적으로 추적하는 표가 있습니다. 이상적으로 나는 출력물을 아래처럼 만들 것이지만, 다른 제안들에 대해서는 완전히 열어두고 싶다.맞춤 시간 설정 사이에 항목 합계

Table - Attendance 
Att_item timestamp 
1  2012-09-12 18:08:00 
2  2012-09-01 23:26:00 
3  2012-09-23 09:33:00 
4  2012-09-11 09:43:00 
5  2012-09-06 05:57:00 
6  2012-09-17 19:26:00 
7  2012-09-06 10:51:00 
8  2012-09-19 09:42:00 
9  2012-09-06 13:55:00 
10  2012-09-05 07:26:00 
11  2012-09-02 03:08:00 
12  2012-09-19 12:17:00 
13  2012-09-12 18:14:00 
14  2012-09-12 18:14:00 

Output 
Date  Timeslot_5pm Timeslot_6pm Timeslot_7pm 
9/11/2012  11   22    22 
9/12/2012  30   21    55 
9/13/2012  44   33    44 

답변

0

귀하의 요구 사항은 완전히 명확하지 않다,하지만 당신은 단지 20 분 창에 레코드의 수를 계산하려면 : 각 시간 이내에 레코드의 수를 계산하려면

select cast(tstmp as date) date, 
    sum(case when datepart(hour, tstmp) = 1 then 1 else 0 end) Timeslot_1am, 
    sum(case when datepart(hour, tstmp) = 2 then 1 else 0 end) Timeslot_2am, 
    sum(case when datepart(hour, tstmp) = 3 then 1 else 0 end) Timeslot_3am, 
    sum(case when datepart(hour, tstmp) = 4 then 1 else 0 end) Timeslot_4am, 
    sum(case when datepart(hour, tstmp) = 5 then 1 else 0 end) Timeslot_5am, 
    sum(case when datepart(hour, tstmp) = 6 then 1 else 0 end) Timeslot_6am, 
    sum(case when datepart(hour, tstmp) = 7 then 1 else 0 end) Timeslot_7am, 
    sum(case when datepart(hour, tstmp) = 8 then 1 else 0 end) Timeslot_8am, 
    sum(case when datepart(hour, tstmp) = 9 then 1 else 0 end) Timeslot_9am, 
    sum(case when datepart(hour, tstmp) = 10 then 1 else 0 end) Timeslot_10am, 
    sum(case when datepart(hour, tstmp) = 11 then 1 else 0 end) Timeslot_11am, 
    sum(case when datepart(hour, tstmp) = 12 then 1 else 0 end) Timeslot_12pm, 
    sum(case when datepart(hour, tstmp) = 13 then 1 else 0 end) Timeslot_1pm, 
    sum(case when datepart(hour, tstmp) = 14 then 1 else 0 end) Timeslot_2pm, 
    sum(case when datepart(hour, tstmp) = 15 then 1 else 0 end) Timeslot_3pm, 
    sum(case when datepart(hour, tstmp) = 16 then 1 else 0 end) Timeslot_4pm, 
    sum(case when datepart(hour, tstmp) = 17 then 1 else 0 end) Timeslot_5pm, 
    sum(case when datepart(hour, tstmp) = 18 then 1 else 0 end) Timeslot_6pm, 
    sum(case when datepart(hour, tstmp) = 19 then 1 else 0 end) Timeslot_7pm, 
    sum(case when datepart(hour, tstmp) = 20 then 1 else 0 end) Timeslot_8pm, 
    sum(case when datepart(hour, tstmp) = 21 then 1 else 0 end) Timeslot_9pm, 
    sum(case when datepart(hour, tstmp) = 22 then 1 else 0 end) Timeslot_10pm, 
    sum(case when datepart(hour, tstmp) = 23 then 1 else 0 end) Timeslot_11pm 
from yourtable 
where datepart(minute, tstmp) >= 50 
    or datepart(minute, tstmp) <= 10 
group by cast(tstmp as date) 

더하기> = 50 및 < = 10 시간 프레임에있는 레코드가 있으면이 값을 조정해야합니다.

0

이것은 하나의 열을 나타냅니다 (물론 4 점이 있습니다).

select DATEPART(YYYY, FTSdate) as [year], DATEPART(mm, FTSdate) as [month] 
    , DATEPART(dd, FTSdate) as [day], DATEPART(hh, FTSdate) as [hour], COUNT(*) 
    from [Gabe2a].[dbo].[docSVsys] 
    where DATEPART(mi, FTSdate) >= 50 or DATEPART(mi, FTSdate) <= 10 
    group by DATEPART(YYYY, FTSdate), DATEPART(mm, FTSdate), DATEPART(dd, FTSdate), DATEPART(hh, FTSdate) 
    order by DATEPART(YYYY, FTSdate), DATEPART(mm, FTSdate), DATEPART(dd, FTSdate), DATEPART(hh, FTSdate) 

구분 열.

select DATEPART(YYYY, FTSdate) as [year], DATEPART(mm, FTSdate) as [month] 
    , DATEPART(dd, FTSdate) as [day] 
    , sum(case when DATEPART(hh, FTSdate) = '0' then 1 else 0 end) as [0:00] -- midnight 
    , sum(case when DATEPART(hh, FTSdate) = '1' then 1 else 0 end) as [1:00] 
    , sum(case when DATEPART(hh, FTSdate) = '2' then 1 else 0 end) as [2:00] 
    , sum(case when DATEPART(hh, FTSdate) = '3' then 1 else 0 end) as [3:00] 
    , sum(case when DATEPART(hh, FTSdate) = '4' then 1 else 0 end) as [4:00] 
    from [Gabe2a].[dbo].[docSVsys] 
    where DATEPART(mi, FTSdate) >= 50 or DATEPART(mi, FTSdate) <= 10 
    group by DATEPART(YYYY, FTSdate), DATEPART(mm, FTSdate), DATEPART(dd, FTSdate) 
    order by DATEPART(YYYY, FTSdate), DATEPART(mm, FTSdate), DATEPART(dd, FTSdate)