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
4

2 に答える 2

0

これはたった1つの列を実行します(まあ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)
于 2012-09-25T22:34:50.157 に答える
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 以下の時間枠にあるレコードをカウントする場合は、これを調整する必要があります。

于 2012-09-25T23:27:28.137 に答える