SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Subtotals
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ImTheodore
Starting Member

16 Posts

Posted - 05/14/2012 :  11:13:08  Show Profile  Reply with Quote

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
Starting Member

USA
26 Posts

Posted - 05/14/2012 :  11:44:44  Show Profile  Reply with Quote
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 - 05/14/2012 :  12:57:31  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000