2012-09-25 36 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) 
相關問題