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.
| Author |
Topic |
|
mrsoleil78
Starting Member
2 Posts |
Posted - 2012-03-17 : 21:12:56
|
| Hi AllI have the following table01-03-2012 09:12:38 A01-03-2012 10:15:23 B01-03-2012 11:21:10 A02-03-2012 08:38:34 A02-03-2012 09:56:59 C03-03-2012 14:45:29 BI need to be able to go from the above table to the below table.Date Room Count01-03-2012 A 201-03-2012 B 102-03-2012 A 102-03-2012 C 103-03-2012 B 1I tried the followingselect t.date,t.room,count(t.room)as "count per room"from tableA tgroup by t.date,t.roombut I get the following table but the count is not rightDate room count per room01-03-2012 09:12:38 A 1 01-03-2012 10:15:23 B 101-03-2012 11:21:10 A 102-03-2012 08:38:34 A 102-03-2012 09:56:59 C 103-03-2012 14:45:29 B 1Any help will be greatly appreciated |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2012-03-17 : 21:24:17
|
| If you convert to date rather than datetime in your select statement, you'll be able to group it by the dates as in your desired output like so:select convert(date, Somedate), room, COUNT(*)from YourTablegroup by convert(date, Somedate), roomorder by SomeDate, Room |
 |
|
|
mrsoleil78
Starting Member
2 Posts |
Posted - 2012-03-17 : 21:56:11
|
| Great stuff!Thank you Flamblaster, it works! |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2012-03-17 : 22:00:35
|
| You're welcome :) Sidenote...this wouldn't work in SQL Server 2005 or lower...you'd have to approach a bit differently because the "date" date type didn't exist until 2008. |
 |
|
|
|
|
|