SQL 服务器:特定范围内的数据

SQL Server : data between specific range

我有一个数据是这样的

stories  value
--------------------------
0        2194940472.78964
1        1651820586.1447
2        627935051.75
3        586994698.4272
4        89132137.57
5        134608008
6        40759564
7        0
8        0
10       0
11       0
12       0
13       26060602
17       0
18       0
19       84522335
20       316478066.045
24       0

我想按范围

求和

我期望的输出

stories            value
0-3                125201021
4-7                215453123
8-12               453121545
12-max(numstories) 21354322

我试过了,但没能弄清楚哪里出了问题

select t.NumStories, SUM(t.bldnvalue) 
from 
    (select 
         a.NumStories,
         case 
            when a.NumStories between 0 and 3 then sum(a.BldgValue)
            when a.NumStories between 4 and 7 then sum(a.BldgValue)
            when a.NumStories between 8 and 12 then sum(a.BldgValue)
            when a.NumStories between 13 and max(a.NumStories) then sum(a.BldgValue)
         end as bldnvalue 
     from 
         dbo.EDM_CocaCola_Coca_Cola_Company_1_1 a 
     group by 
         a.NumStories) t
group by 
    t.NumStories

通过这个查询,我得到了这个输出

NumStories  value
-------------------------------
0           2194940472.78964
3           586994698.4272
12          0
6           40759564
7           0
1           1651820586.1447
24          0
18          0
10          0
4           89132137.57
19          84522335
13          26060602
5           134608008
2           627935051.75
17          0
11          0
20          316478066.045
8           0 

你可以试试这个:

SELECT '0-3' AS stories,
       SUM(value) AS value
FROM dbo.EDM_CocaCola_Coca_Cola_Company_1_1
WHERE stories BETWEEN 0 AND 3

UNION ALL

SELECT '4-7' AS stories,
       SUM(value) AS value
FROM dbo.EDM_CocaCola_Coca_Cola_Company_1_1
WHERE stories BETWEEN 4 AND 7

UNION ALL

...

只需首先构建您想要的分组字符串,然后按该变量分组。

select 
     case 
        when a.NumStories between 0 and 3 then '0-3'
        when a.NumStories between 4 and 7 then '4-7'
        when a.NumStories between 8 and 12 then '8-12'
        when a.NumStories >= 13 then '13-max'
     end as stories,
     sum(a.BldgValue) as value
 from 
     dbo.EDM_CocaCola_Coca_Cola_Company_1_1 a 
 group by 1;

如果你真的想也打印最大值,那么你可以在“13-max”行中放入一个子查询 (SELECT MAX(BldgValue) FROM dbo.EDM_CocaCola_Coca_Cola_Company_1_1)

这是 CTE 的解决方案,无需复制代码即可适用于任何数据集。

declare @YourTable table(stories int, value money)
declare @GroupMemberCount int=4

insert @YourTable (stories,value) values (0,5),(1,10),(2,11),(3,7),(4,18),(5,13),(7,15)

;with cte as
(
    select c.stories+v.i*@GroupMemberCount FirstGroupMember, c.stories+v.i*@GroupMemberCount+@GroupMemberCount -1 LastGroupMember
        ,CAST(c.stories+v.i*@GroupMemberCount as varchar(50))
            +'-'+CAST(c.stories+v.i*@GroupMemberCount+@GroupMemberCount -1 as varchar(50))GroupName
    from (select MIN(stories) stories from @YourTable) c
        cross join (values (0),(1),(2),(3),(4)/* and so on */) v(i)
    where exists (select * from @YourTable yt where yt.stories>=c.stories+v.i*3)
)
select c.GroupName, SUM(yt.value)
from cte c
     JOIN @YourTable yt ON yt.stories BETWEEN c.FirstGroupMember AND C.LastGroupMember
GROUP BY c.GroupName

我喜欢这个结果,我尝试使用BIN的概念。我认为唯一的问题是你的 max bin。我不明白你是如何得到输出总和的。第一个记录值是 '2,194,940,472.78964',它比你在 0-3 bin

中的值大
if OBJECT_ID('tempdb..#Test') is not null
    drop table #Test;

Create table #Test (
                        Stories int
                        , Value float
                    )
insert into #Test
values
 (0 , 2194940472.78964)
, (1 , 1651820586.1447 )
, (2 , 627935051.75    )
, (3 , 586994698.4272  )
, (4 , 89132137.57     )
, (5 , 134608008       )
, (6 , 40759564        )
, (7 , 0               )
, (8 , 0               )
, (10, 0               )
, (11, 0               )
, (12, 0               )
, (13, 26060602        )
, (17, 0               )
, (18, 0               )
, (19, 84522335        )
, (20, 316478066.045   )
, (24, 0               )



if OBJECT_ID('tempdb..#Bins') is not null
    drop table #Bins;
create Table #Bins(
                        Label varchar(20)
                        , Min int
                        , Max int
                    )

insert into #Bins values
 ('0-3', 0, 3)
, ('4-7', 4, 7)
, ('8-12', 8, 12)
, ('13 - Max', 13, 999999999)

Select b.Label
    , sum(t.Value) as Value
from #Test t 
join #Bins b
    on t.stories between b.Min and b.Max
Group by b.Label
order by 1

输出:

Label                Value
-------------------- ----------------------
0-3                  5061690809.11154
13 - Max             427061003.045
4-7                  264499709.57
8-12                 0