如何对列值求和并在另一列中更新这些总和值

时间:2017-02-03 06:32:37

标签: sql-server

我有一个表和这样的值,我想要输出如下图所示,首先我需要按区域排序然后我必须根据区域排序求和mhs_count然后我需要更新它在total_sum中最后对应的row-side为total_sum,如图所示

create table AreaSummary1( area varchar(250),mhs_count int, total_sum int) 

insert into AreaSummary1(area,mhs_count) values ('AAA',1)
insert into AreaSummary1(area,mhs_count) values ('BBB',2)
insert into AreaSummary1(area,mhs_count) values ('CCC',3)
insert into AreaSummary1(area,mhs_count) values ('AAA',4)
insert into AreaSummary1(area,mhs_count) values ('BBB',5)
insert into AreaSummary1(area,mhs_count) values ('CCC',6)
insert into AreaSummary1(area,mhs_count) values ('AAA',7)
insert into AreaSummary1(area,mhs_count) values ('BBB',8)
insert into AreaSummary1(area,mhs_count) values ('CCC',9)

enter image description here

5 个答案:

答案 0 :(得分:0)

您可以获得col mhs_count的总和,如:

SELECT SUM(mhs_count) AS Totalcount FROM yourTableName WHERE area='AAA'

//Output : 12 

同样适用于其他领域。

PS:如果您尝试将计算值存储在表格中,请告诉您这不是一个好习惯,您必须始终在前端显​​示之前计算值。

这将始终返回更新的值。

答案 1 :(得分:0)

您的意思是将total_sum更新为具有maxium mhs_count

的行
update a set a.total_sum=b.total_sum from AreaSummary1 as a
inner join (select a1.area, sum(a1.mhs_count) as total_sum,max(a1.mhs_count) as max_mhs from AreaSummary1 as a1 group by a1.area) as b
on a.area=b.area and a.mhs_count=b.max_mhs

select * from AreaSummary1 order by area,mhs_count
area    mhs_count   total_sum
AAA 1   NULL
AAA 4   NULL
AAA 7   12
BBB 2   NULL
BBB 5   NULL
BBB 8   15
CCC 3   NULL
CCC 6   NULL
CCC 9   18

按实际地址:

这是按地址的示例订单(使用%% lockres %%) 如果要更改排序列,只需更改ROW_NUMBER中的ORDER BY

CREATE table AreaSummary1( area varchar(250),mhs_count int, total_sum int) 

insert into AreaSummary1(area,mhs_count) values ('AAA',1)
insert into AreaSummary1(area,mhs_count) values ('BBB',8) --changed the store sequence
insert into AreaSummary1(area,mhs_count) values ('CCC',3)
insert into AreaSummary1(area,mhs_count) values ('AAA',4)
insert into AreaSummary1(area,mhs_count) values ('BBB',5)
insert into AreaSummary1(area,mhs_count) values ('CCC',6)
insert into AreaSummary1(area,mhs_count) values ('AAA',7)
insert into AreaSummary1(area,mhs_count) values ('BBB',2) ---changed the sequence
insert into AreaSummary1(area,mhs_count) values ('CCC',9)

update a set a.total_sum=aa.total_sum 
--SELECT * 
FROM AreaSummary1 as a
CROSS apply ( SELECT SUM(b.mhs_count) AS total_sum,MAX(b.locid) AS locid FROM (
SELECT a1.mhs_count ,CASE WHEN COUNT(0)OVER()=ROW_NUMBER()OVER(ORDER BY a1.%%lockres%%) THEN   (a1.%%lockres%%) ELSE '' END  AS locid
from AreaSummary1 as a1 WHERE  a1.area=a.area) as b
) aa
WHERE aa.locid= a.%%lockres%%

select area,mhs_count,ISNULL(LTRIM(total_sum),'') AS total_sum from AreaSummary1 order by area,%%lockres%%
area    mhs_count   total_sum
AAA 1   
AAA 4   
AAA 7   12
BBB 8   
BBB 5   
BBB 2   15
CCC 3   
CCC 6   
CCC 9   18

答案 2 :(得分:0)

您可以这样做:

SELECT *
into #t
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY area ORDER BY area DESC) AS Seq
,*
FROM #AreaSummary1
)t


select x.area
    ,x.mhs_count
    ,case when convert(varchar(10),x.total_sum) = 0 then ''
    else x.total_sum end as total_sum
from(
select t.area
    ,t.mhs_count
    ,case when t.Seq = c.lastMember then b.total_sum
        else '' end as total_sum
from #t t
left outer join (select area        
                ,sum(mhs_count) as total_sum
            from #t
            group by area) b on b.area = t.area
left outer join (select area
                ,max(Seq) as lastMember
            from #t
            group by area) c on c.area = t.area
) x

答案 3 :(得分:0)

使用Row_number我们可以实现这个

Select area,
mhs_count,
CASE WHEN RNN = 3 then RN ELSE NULL END Total_Sum 
from 
(
select 
        area,
        mhs_count,
        SUM(mhs_count)OVER(PARTITION BY area)RN,
        ROW_NUMBER()OVER(PARTITION BY area ORDER BY area)RNN 
    from AreaSummary1
)T

答案 4 :(得分:0)

我使用过Row_number和聚合函数。

SELECT area
       ,mhs_count
       ,CASE WHEN RN<(MAX(RN) OVER(PARTITION BY AREA)) THEN 0 ELSE TOT END AS total_sum
FROM 
(SELECT area,mhs_count,ROW_NUMBER() OVER(PARTITION BY AREA ORDER BY AREA) RN,sum(mhs_count) over(partition by area) TOT
FROM AreaSummary1) X