user1254053
user1254053

Reputation: 765

SQL Server - get sum of values in a range

I have a table with values like this:

Date(in YMD)            Column1        Column2
2013-08-08              0               6
2013-08-08              1               87
2013-08-08              2.8             23
2013-08-08              3.87            12
2013-08-08              0.111           65
2013-08-08              5               98
2013-08-08              7.876           67
2013-08-09              4.32            8
2013-08-09              4               2   
2013-08-09              76.05           100
2013-08-09              32              9

I want to get the sum of column2 in range of column1 (i.e. add column2 values which falls in column1 range). So the result would be like this:

[0-1]               158
[1-2]               0
[2-3]               23
[3-4]               14
[4-5]               106
[5-6]               0
[6-7]               0
[7-8]               67
[8-9]               0
[10+]               109

Initially i was getting this result with the below query and it was working fine.

        SELECT [Range], [TotalSum] FROM
            (
                SELECT
                    CASE WHEN Column1 >= 0 AND Column1 <= 1 THEN '[0-1]'
                        WHEN Column1 > 1 AND Column1 <= 2 THEN '[1-2]'
                        WHEN Column1 > 2 AND Column1 <= 3 THEN '[2-3]'
                        WHEN Column1 > 3 AND Column1 <= 4 THEN '[3-4]'              
                        WHEN Column1 > 4 AND Column1 <= 5 THEN '[4-5]'
                        WHEN Column1 > 5 AND Column1 <= 6 THEN '[5-6]'
                        WHEN Column1 > 6 AND Column1 <= 7 THEN '[6-7]'
                        WHEN Column1 > 7 AND Column1 <= 8 THEN '[7-8]'
                        WHEN Column1 > 8 AND Column1 <= 9 THEN '[8-9]'
                        WHEN Column1 > 9 AND Column1 <= 10 THEN '[9-10]'                                
                        ELSE '[10+]' END [Range],
                        SUM(Column2) [TotalSum]
                FROM dbo.table      
                GROUP BY
                    CASE WHEN Column1 >= 0 AND Column1 <= 1 THEN '[0-1]'
                        WHEN Column1 > 1 AND Column1 <= 2 THEN '[1-2]'
                        WHEN Column1 > 2 AND Column1 <= 3 THEN '[2-3]'
                        WHEN Column1 > 3 AND Column1 <= 4 THEN '[3-4]'
                        WHEN Column1 > 4 AND Column1 <= 5 THEN '[4-5]'
                        WHEN Column1 > 5 AND Column1 <= 6 THEN '[5-6]'
                        WHEN Column1 > 6 AND Column1 <= 7 THEN '[6-7]'
                        WHEN Column1 > 7 AND Column1 <= 8 THEN '[7-8]'
                        WHEN Column1 > 8 AND Column1 <= 9 THEN '[8-9]'
                        WHEN Column1 > 9 AND Column1 <= 10 THEN '[9-10]'                
                        ELSE '[10+]' END
            ) t1    
            ORDER BY
            CASE [Range]
                        WHEN '[0-1]' THEN 0
                        WHEN '[1-2]' THEN 1
                        WHEN '[2-3]' THEN 2
                        WHEN '[3-4]' THEN 3
                        WHEN '[4-5]' THEN 4
                        WHEN '[5-6]' THEN 5
                        WHEN '[6-7]' THEN 6
                        WHEN '[7-8]' THEN 7
                        WHEN '[8-9]' THEN 8
                        WHEN '[9-10]' THEN 9                
                        WHEN '[10+]' THEN 10
            END

Now i have a requirement where i have to apply a filter, here for eg say "Where Date between '2013-08-09' and '2013-08-09' " Though this return the correct values but only for range where data is available i.e. [3-4], [4-5] , and [10+]

Please can you suggest is there a way to get all the range and to show 0 in TotalSum column where there are no rows for that range.

What changes should i make in this query to show all range values with values in TotalSum column(it should either be 0 or sum of column2 for that range).

If you any other idea to achieve this, please suggest. Any help would be much appreciated.

Upvotes: 2

Views: 5492

Answers (1)

roman
roman

Reputation: 117370

with cte as (
   select 0 as rng_start, 1 as rng_end
   union all
   select
      rng_end as rng_start,
      case when rng_end = 10 then null else rng_end + 1 end as rng_end
   from cte
   where rng_end <= 10
), cte2 as (
   select
       '[' + cast(c.rng_start as nvarchar(max)) +
       isnull('-' + cast(c.rng_end as nvarchar(max)), '+') + ']' as rng_str,
       case when c.rng_start = 0 then -1 else c.rng_start end as rng_start,
       c.rng_end
   from cte as c
)
select
    c.rng_str,
    isnull(sum(t.column2), 0)
from cte2 as c
    left outer join Table1 as t on
        t.column1 > c.rng_start and
        (t.column1 <= c.rng_end or c.rng_end is null) and
        t.[Date] between '2013-08-09' and '2013-08-09'   
group by c.rng_str, c.rng_start
order by c.rng_start

sql fiddle demo

Upvotes: 1

Related Questions