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 2005 Forums
 Transact-SQL (2005)
 Grouping in a range

Author  Topic 

ymamalis
Starting Member

42 Posts

Posted - 2009-12-13 : 12:31:17
Hello everybody, i have a query that calculates a field named ss (it counts the days difference between 2 days) how can i take results grouped in a range of days , i mean i would like to count how many records do i have with ss column between 0-10 , 11-40 , 41-100 e.t.c.
Can you please help me?
Wish result Table
ssCount Range
34 1-10
78 11-40
132 41-1000

singularity
Posting Yak Master

153 Posts

Posted - 2009-12-13 : 13:22:05
[code]
select case when ss between 1 and 10 then '1-10'
when ss between 11 and 40 then '11-40'
when ss between 41 and 1000 then '41-1000' end as range,
count(*) as ssCount
from yourtable
group by case when ss between 1 and 10 then '1-10'
when ss between 11 and 40 then '11-40'
when ss between 41 and 1000 then '41-1000' end
[/code]
Go to Top of Page
   

- Advertisement -