Tsql 汇总自定义时间设置之间的项目
我们需要按天计算小时前10分钟和小时后10分钟发生的项目数。我们有一个单独跟踪项目的表。理想情况下,我希望输出如下所示,但完全接受其他建议Tsql 汇总自定义时间设置之间的项目,tsql,Tsql,我们需要按天计算小时前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
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,但你明白我的意思)
选择日期部分(YYYY,FTSdate)作为[年],日期部分(mm,FTSdate)作为[月]
,DATEPART(dd,FTSdate)为[天],DATEPART(hh,FTSdate)为[小时],COUNT(*)
来自[Gabe2a].[dbo].[docSVsys]
如果DATEPART(mi,FTSdate)>=50或DATEPART(mi,FTSdate)=50或DATEPART(mi,FTSdate)您的要求并不完全清楚,但如果您只想计算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)
选择cast(tstmp作为日期)日期,
总和(datepart(小时,tstmp)=1,然后1,否则0结束时的情况)时间段凌晨1点,
总和(datepart(小时,tstmp)=2,然后1,否则0结束时的情况)时间段凌晨2点,
总和(当datepart(小时,tstmp)=3时,则为1,否则为0结束)时间段凌晨3点,
总和(datepart(小时,tstmp)=4,然后1,否则0结束时的情况)时隙,
总和(当datepart(小时,tstmp)=5时,则为1,否则为0结束)时隙_5am,
总和(当datepart(小时,tstmp)=6时,则为1,否则为0结束)时间段上午6点,
总和(当datepart(小时,tstmp)=7时,则为1,否则为0结束)时隙_7am,
总和(当datepart(小时,tstmp)=8时,则为1,否则为0结束)时隙_8am,
总和(当datepart(小时,tstmp)=9时,则为1,否则为0结束)时间段上午9时,
总和(当datepart(小时,tstmp)=10时,则为1,否则为0结束)时间段上午10时,
总和(当datepart(小时,tstmp)=11时,则为1,否则为0结束)时隙\u 11am,
总和(当datepart(小时,tstmp)=12时,则为1,否则为0结束)时隙\u 12pm,
总和(当datepart(小时,tstmp)=13时,则为1,否则为0结束)时隙\u 1pm,
总和(当datepart(小时,tstmp)=14时,则为1,否则为0结束)时隙下午2点,
总和(当datepart(小时,tstmp)=15,然后1或0结束时的情况)时间段下午3点,
总和(当datepart(小时,tstmp)=16时,则为1,否则为0结束)时间段下午4点,
总和(当datepart(小时,tstmp)=17时,则为1,否则为0结束)时间段下午5点,
总和(当datepart(小时,tstmp)=18时,则为1,否则为0结束)时隙_6pm,
总和(当datepart(小时,tstmp)=19时,则为1,否则为0结束)时隙_7pm,
总和(当datepart(小时,tstmp)=20时,则为1,否则为0结束)时隙_8pm,
总和(当datepart(小时,tstmp)=21时,则为1,否则为0结束)时隙_9pm,
总和(当datepart(小时,tstmp)=22时,则为1,否则为0结束)时隙\u 10pm,
总和(当datepart(小时,tstmp)=23时,则为1,否则为0结束)时隙\u 11pm
从你的桌子上
其中datepart(分钟,tstmp)>=50
或日期部分(分钟,tstmp)=50和
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)
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)