22,209
社区成员
发帖
与我相关
我的任务
分享
select substring(convert(varchar(50),time,120),1,10),substring(convert(varchar(50),time,120),12,2),count(*)
from table
group by substring(convert(varchar(50),time,120),1,10),substring(convert(varchar(50),time,120),12,2)
order by 1,2
with a(time,title) as (
select '2016-03-22 01:02:00.000','a' union all
select '2016-03-22 02:02:00.000','b' union all
select '2016-03-22 09:02:00.000','x')
select aa.number,isnull(bb.value,0) as value from
(select number+1 as number from master..spt_values where type='p' and number<24) as aa left join
(select DATEPART(hh,time) as time,COUNT(*) as value from a group by DATEPART(hh,time)) as bb on
aa.number=bb.time
with tests(time,title) as (
select '2016-03-22 01:02:00.000','a' union all
select '2016-03-22 02:02:00.000','b' union all
select '2016-03-22 09:02:00.000','x'
),lst as (
select number from master..spt_values where type='p' and number<24
),t as (
select datepart(HH,tests.time) h,COUNT(1) cnt
from tests
where time between CAST(GETDATE() AS date) and GETDATE()
group by datepart(HH,tests.time)
)
select lst.number,isnull(t.cnt,0) value from lst left join t on lst.number=t.h