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 2008 Forums
 Transact-SQL (2008)
 noob to grouping question: ranges 10-20,21-30,31-

Author  Topic 

SMerrill
Posting Yak Master

206 Posts

Posted - 2009-11-24 : 17:38:50
SELECT * FROM #Temp
TrackingID DaysBetweenEvents
1 1
2 4
3 3
4 30
5 17
6 19
7 17
.
.
.

I want a statistical counting of the above:
with columns <0, 0-10, 11-20, 21-30, 31+

I know there are new fancy ways to compute the count in each numeric-range bin. Please give me one such technique.


~ Shaun Merrill
Seattle, WA

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-25 : 02:02:06
Like this

select
sum(case when DaysBetweenEvents between 0 and 10 then 1 else o end) as [0-10],
sum(case when DaysBetweenEvents between 11 and 20 then 1 else o end) as [11-20],
.
.
from your_table


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -