Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I have a query displaying just numeric datae.g 51034502However 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 SQLThanks
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"
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 IntervalFROM myTable) AGROUP BY Interval
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2012-04-28 : 19:21:54
I am using Microsoft Access to write my SQLthen you should have posted this access forumsThough 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 MVPhttp://visakhm.blogspot.com/
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 myTableGROUP 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")