[英]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 (ie add column2 values which falls in column1 range). 我想得到column1范围内column2的总和(即,添加落在column1范围内的column2值)。 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 ie [3-4], [4-5] , and [10+] 现在,我有一个必须在其中应用过滤器的要求,例如在此处说“ Where Date between'2013-08-09'和'2013-08-09'”尽管这将返回正确的值,但仅适用于数据为可用,例如[3-4],[4-5]和[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. 请您建议是否有一种方法可以获取所有范围,并在TotalSum列中显示0,其中该范围没有行。
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). 我应该在此查询中进行哪些更改以显示所有范围值以及TotalSum列中的值(该范围应为0或column2的总和)。
If you any other idea to achieve this, please suggest. 如果您有其他实现此目标的想法,请提出建议。 Any help would be much appreciated.
任何帮助将非常感激。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.