SQL Server - 获取范围内的值的总和

时间:2013-08-23 04:49:49

标签: sql sql-server sql-server-2008 tsql

我有一个像这样的值的表:

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

我想在column1的范围内得到column2的总和(即添加列在column1范围内的column2值)。所以结果是这样的:

[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

最初我用以下查询得到了这个结果,它运行正常。

        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

现在我有一个要求,我必须应用过滤器,例如说“2013-08-09'和'2013-08-09'之间的日期” 虽然这会返回正确的值,但仅适用于数据可用的范围,即[3-4],[4-5]和[10+]

请问您是否有办法获得所有范围并在TotalSum列中显示0,其中该范围没有行。

我应该在此查询中做出哪些更改,以显示TotalSum列中包含值的所有范围值(它应该是0或该范围的column2的总和)。

如果您有任何其他想法,请建议。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

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