March 13, 2014 at 10:54 am
Create table Clms(
ID varchar(11),
ServiceDate decimal(8,0))
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344455501', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344466601', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
INSERT INTO CLMS (ID, SERVICEDATE) VALUES('33344477701', 20140101)
Based on this data, I need to form results like this
Month Count Range
JAN 1 1-25
JAN 1 26-50
JAN 0 51-75
JAN 1 76-100
SQL Help needed
March 13, 2014 at 11:06 am
Could you explain the logic to obtain the expected results?
March 13, 2014 at 11:31 am
From the data in the table, I need to count the number of ID's that fall into a count range or bucket
which is incremented by 25, i.e. 1-25, 25-50, 51-75, 76-100......
by Month, which will be Jan and February 2014
March 13, 2014 at 12:05 pm
Do a favor to yourself and use date data types when needed. Using numeric values (or even string values) will only give you headaches.
Here's a possible solution for you (I'm assuming that you don't have a ranges table, but you really should have one):
WITH groupedCLMS AS(
SELECT ID, LEFT(ServiceDate, 6) ServiceMonth, COUNT(*) cnt, COUNT(*) / 25 grouper
FROM Clms
GROUP BY ID, LEFT(ServiceDate, 6)
)
SELECT STUFF( CONVERT(char(11), CONVERT( date, ServiceMonth + '01'), 100), 4, 3, '') ServiceMonth,
SUM(CASE WHEN grouper = N THEN 1 ELSE 0 END),
CAST( (N * 25) + 1 AS varchar(5)) + '-' + CAST( (N + 1) * 25 AS varchar(5))
FROM groupedCLMS g
CROSS
JOIN (VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9)) Tally(N)
GROUP BY Tally.N, ServiceMonth
March 13, 2014 at 2:41 pm
Worked like magic... Thank you for your help!
March 13, 2014 at 2:46 pm
It's great, but do you understand how does it work?
You'll be the one in charge of this for any modification, correction or improvement, so you better understand it. Feel free to ask any questions you have.
March 13, 2014 at 3:13 pm
Understood and thanks again. I had to expand the groupings to 30 to account for all the buckets we found.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply