Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Pivot table help..

Author  Topic 

aiken
Aged Yak Warrior

525 Posts

Posted - 2003-08-12 : 14:21:16
Ok, I know enough to know the general theory of how to do this, but my brain is about to explode after staring at it for too long. Help!

Given:
CREATE TABLE USERS (user_id int PRIMARY KEY, signed up datetime)


I want to know how many users signed up during 1 hour periods, as measured on a couple of days. So my desired results are something like:
	10-Aug	11-Aug	12-Aug
6am 17 12 17
7am 21 28 21
8am 25 22 23
9am 21 24 30
10am 28 26 52


..Clearly this is a situation for counting cases, but I just cannot get it.

Help, please?

Thanks
-b

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2003-08-12 : 14:44:33
create table t (id int, dt datetime)

select datepart(hh, dt) hour,

sum(case when datepart(dd, dt)=10 then 1 else 0 end) Aug10,
sum(case when datepart(dd, dt)=11 then 1 else 0 end) Aug11,
sum(case when datepart(dd, dt)=12 then 1 else 0 end) Aug12

from t where datepart(mm, dt)=8

group by datepart(hh, dt)
Go to Top of Page
   

- Advertisement -