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
 Can you group numeric data into intervals?

Author  Topic 

mrdatabase
Starting Member

12 Posts

Posted - 2012-04-28 : 09:15:47
I have a query displaying just numeric data

e.g

5
10
3
4
5
0
2

However i would like to group this data into intervals say, how many of these results: equals 0, are between 1 and 3, are between 4 and 7, between 8 and 14, between 15 - 21, between 22 and infinity.


Is it possible to create these intervals and have a count of the which numbers fit into which spec?

I am using Microsoft Access to write my SQL

Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-04-28 : 11:02:56
Yes. Just divide with the interval you are investigating.
Or have an auxiliary table with two column; FromValue and ToValue, and join against this table.


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-04-28 : 14:09:14
Another method:
SELECT Interval, Count(*) FROM (SELECT CASE WHEN num<=0 THEN '0'
WHEN num<=3 THEN '1-3'
WHEN num<=7 THEN '4-7'
WHEN num<=14 THEN '8-14'
WHEN num<=21 THEN '15-21'
ELSE '22-infinity' END AS Interval
FROM myTable) A
GROUP BY Interval
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-28 : 19:21:54
I am using Microsoft Access to write my SQL
then you should have posted this access forums

Though most sql queries work in both access and t-sql there are few variations.Also solutions given here might be mostly t-sql based

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-04-28 : 22:03:22
quote:
I am using Microsoft Access to write my SQL
Someday...mark my words...I will actually READ the question before I post an answer.

Here's an Access version:
SELECT Switch(num<=0,"0",num<=3,"1-3",num<=7,"4-7",num<=14,"8-14",num<=21,"15-21",num>=22,"22-infinity") AS Interval,
Count(*)
FROM myTable
GROUP BY Switch(num<=0,"0",num<=3,"1-3",num<=7,"4-7",num<=14,"8-14",num<=21,"15-21",num>=22,"22-infinity")
Go to Top of Page
   

- Advertisement -