34,837
社区成员




declare @dt table(id int identity(1,1), [type] int, userid int, DateID int, HourID int)
-- type 观看类型,
insert into @dt values
(1,1,20120720, 15),
(2,2,20120715, 23),
(3,3,20120713, 5),
(1,4,20120716, 4),
(2,1,20120714, 14),
(3,2,20120720, 17),
(1,2,20120718, 19),
(2,4,20120710, 5),
(3,4,20120720, 8),
(1,3,20120718, 15),
(2,5,20120719, 20)
select * from @dt
declare @dt table(id int identity(1,1), [type] int, userid int, DateID int, HourID int)
-- type 观看类型,
insert into @dt values
(1,1,20120720, 15),
(2,2,20120715, 23),
(3,3,20120713, 5),
(1,4,20120716, 4),
(2,1,20120714, 14),
(3,2,20120720, 17),
(1,2,20120718, 19),
(2,4,20120710, 5),
(3,4,20120720, 8),
(1,3,20120718, 15),
(2,5,20120719, 20)
select
SUM(case when HourID between 17 and 22 then 1 else 0 end) as [17-22]
,SUM(case when HourID between 0 and 4 or HourID =23 then 1 else 0 end) as [23-4]
,SUM(case when HourID between 5 and 10 then 1 else 0 end) as [5-10]
,SUM(case when HourID between 11 and 16 then 1 else 0 end) as [11-16]
from @dt where type in (1,2,3)
group by type
/*
17-22 23-4 5-10 11-16
----------- ----------- ----------- -----------
1 1 0 2
1 1 1 1
1 0 2 0
*/
--老兄,为别人提供语句好歹也建个正确的,没看明白你的153怎么出来的
declare @dt table(id int identity(1,1), [type] int, userid int, DateID int, HourID int)
insert into @dt select 1,1,20120720, 15
union all select 2,2,20120715, 23
union all select 3,3,20120713, 5
union all select 1,4,20120716, 4
union all select 2,1,20120714, 14
union all select 3,2,20120720, 17
union all select 1,2,20120718, 19
union all select 2,4,20120710, 5
union all select 3,4,20120720, 8
union all select 1,3,20120718, 15
union all select 2,5,20120719, 20
select * from @dt