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
 General SQL Server Forums
 New to SQL Server Programming
 Subtotals

Author  Topic 

ImTheodore
Starting Member

16 Posts

Posted - 2012-05-14 : 11:13:08

I need the total for males and females for each hour:

[Sex] [Day] [Time] [Day of week]

M 01-01-06 0:00 Sunday
F 01-01-06 0:00 Sunday
M 01-01-06 0:00 Sunday
M 01-01-06 0:00 Sunday
M 01-01-06 1:00 Sunday
F 01-01-06 1:00 Sunday
M 01-01-06 1:00 Sunday
M 01-01-06 2:00 Sunday
M 01-01-06 3:00 Sunday
F 01-01-06 4:00 Sunday
F 01-01-06 4:00 Sunday
M 01-01-06 4:00 Sunday etc...


Should appear like this:

[Date] [Time] [Day of week] [M] [F]
01-01-06 0:00 Sunday 3 1
01-01-06 1:00 Sunday 2 1
01-01-06 2:00 Sunday 1
01-01-06 3:00 Sunday 1
01-01-06 4:00 Sunday 1 2

Thanks,
Dave

ITTrucker
Yak Posting Veteran

64 Posts

Posted - 2012-05-14 : 11:44:44
Should be something like this:

SELECT [DATE], [TIME], [DAY OF WEEK],
[M] = SUM(CASE WHEN [SEX] = 'M' THEN 1 ELSE 0 END),
[F] = SUM(CASE WHEN [SEX] = 'F' THEN 1 ELSE 0 END)
FROM [TABLENAME]
GROUP BY [DATE], [TIME], [DAY OF WEEK]

Go to Top of Page

ImTheodore
Starting Member

16 Posts

Posted - 2012-05-14 : 12:57:31
I knew it was easier than I was making it out to be (creating and joining temp tables), Thank You very much!
Go to Top of Page
   

- Advertisement -