August 18, 2012 at 6:15 am
I'm trying to create a report that groups by 10. Desired result is range and count.
Thanks
0 = 3
10 = 4
20 = 2
30 = 3
CREATE TABLE #Range
(Amt money)
INSERT INTO #Range (Amt)
SELECT 0 UNION ALL
SELECT 0 UNION ALL
SELECT 0 UNION ALL
SELECT 1.01 UNION ALL
SELECT 3.01 UNION ALL
SELECT 7.01 UNION ALL
SELECT 7.01 UNION ALL
SELECT 11.01 UNION ALL
SELECT 13.01 UNION ALL
SELECT 21.01 UNION ALL
SELECT 23.01 UNION ALL
SELECT 27.01
August 18, 2012 at 6:30 am
Not sure what your expected output is, but the function you want is "ceiling".
SELECT Amt, CEILING(Amt/10)*10 AS groupByTens
FROM #Range;
Returns: -
Amt groupByTens
--------------------- ---------------------
0.00 0.00
0.00 0.00
0.00 0.00
1.01 10.00
3.01 10.00
7.01 10.00
7.01 10.00
11.01 20.00
13.01 20.00
21.01 30.00
23.01 30.00
27.01 30.00
If you want it as counts, then try this: -
SELECT CEILING(Amt/10)*10 AS groupByTens, COUNT(1) AS counter
FROM #Range
GROUP BY CEILING(Amt/10)*10;
Returns: -
groupByTens counter
--------------------- -----------
0.00 3
10.00 4
20.00 2
30.00 3
August 18, 2012 at 6:41 am
perfect, thanks
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply