获取值的总和到表中的下一行

时间:2018-05-22 04:51:31

标签: sql sql-server

我有一个表Parameter_List,其中包含字段Parameter_ID,Parameters_Name,Target_Value,GroupID和Sequence_No。 值可以是

Parameter_ID    Parameters_Name Target_Value    GroupID Sequence_No  
1   Para_1  1000    2   1  
2   Para_2  2000    2   2  
3   Para_3  3000    2   3  
4   Para_4  NULL    2   4  
5   Para_5  5000    2   5  

6   Para_1  1450    3   1  
7   Para_2  1200    3   2  
8   Para_4  NULL    3   3  
9   Para_5  3000    3   4  

10  Para_2  3000    4   1  
11  Para_3  4000    4   2  
12  Para_4  NULL    4   3  

如您所见,Para_4的Target_Value始终为NULL。 现在,当我想显示记录时,我希望Para_4的值为该组的Para_4以上参数的Target_Value之和。
例如,对于GroupID 2,Para_4的值应该是前3行的总和。表有一个Sequence_No列。所以Para_4应该将Target_Value作为该特定组中其上方所有行的总和。 它应该按GroupID分组。所以结果看起来像

Para_1  1000    2
Para_2  2000    2
Para_3  3000    2
Para_4  6000    2
Para_5  5000    2

Para_1  1450    3
Para_2  1200    3
Para_4  2650    3
Para_5  3000    3

5 个答案:

答案 0 :(得分:3)

一种方法是计算CTE中Target_Value的累积总和,然后在Target_ValueNULL的任何地方更新该CTE。

WITH cte AS (
    SELECT GroupID, Sequence_No,
        SUM(Target_Value) OVER (PARTITION BY GroupID
            ORDER BY Sequence_No ROWS UNBOUNDED PRECEDING) target_sum
    FROM yourTable
)

UPDATE cte
SET Target_Value = target_sum
WHERE Target_Value IS NULL;

答案 1 :(得分:1)

with CTE as (
  select 
     [Parameter_ID],[Parameters_Name],
      (
        select sum(Target_Value) from Parameter_List
        where GroupID = T1.GroupID and Parameter_ID < T1.Parameter_ID
      )  as Target_Value
    , [GroupID]
  from Parameter_List T1
  where Target_Value is null

  union all 

  select [Parameter_ID],[Parameters_Name],[Target_Value], [GroupID] from Parameter_List
  where Target_Value is not null
)
select [Parameters_Name],[Target_Value], [GroupID] from CTE 
order by Parameter_ID;
| Parameters_Name | Target_Value | GroupID |
|-----------------|--------------|---------|
|          Para_1 |         1000 |       2 |
|          Para_2 |         2000 |       2 |
|          Para_3 |         3000 |       2 |
|          Para_4 |         6000 |       2 |
|          Para_5 |         5000 |       2 |
|          Para_1 |         1450 |       3 |
|          Para_2 |         1200 |       3 |
|          Para_4 |         2650 |       3 |
|          Para_5 |         3000 |       3 |
|          Para_2 |         3000 |       4 |
|          Para_3 |         4000 |       4 |
|          Para_4 |         7000 |       4 |

DEMO SQL Fiddle

答案 2 :(得分:1)

总结就是你想要的

;with mycte as (
select 1 as parameter_id
,'para_1' as parameter_name
,1000 as target_value
,2 as groupid
,1 as sequence_no

union all

select 2 as parameter_id
,'para_2' as parameter_name
,2000 as target_value
,2 as groupid
,2 as sequence_no

union all

select 3 as parameter_id
,'para_3' as parameter_name
,3000 as target_value
,2 as groupid
,3 as sequence_no

union all

select 4 as parameter_id
,'para_4' as parameter_name
,NULL as target_value
,2 as groupid
,4 as sequence_no

union all

select 5 as parameter_id
,'para_5' as parameter_name
,5000 as target_value
,2 as groupid
,5 as sequence_no
)

Select parameter_name, isnull(target_value, sum (target_value) over(partition by groupid order by sequence_no)) ,groupid from mycte

答案 3 :(得分:1)

参加派对的时间较晚,但这里还有一个解决方案 -

SELECT t.Parameters_Name, t.GroupID,
CASE when t.Target_Value IS NULL THEN 
                    (select sum(Target_Value) as runningTotal
                    from Parameter_List 
                    where GroupID = t.GroupID and Sequence_No <= t.Sequence_No) ELSE t.Target_Value END AS Target_Value  
FROM Parameter_List  t

答案 4 :(得分:0)

您可以尝试新的Applies to: SQL Server 2012 (11.x) through SQL Server 2017

SELECT Parameter_ID, 
    Parameters_Name,
    ISNULL(Target_Value,
        SUM(Target_Value) OVER (PARTITION BY GroupID   
                                ORDER BY Sequence_No  
                                ROWS UNBOUNDED PRECEDING)) AS Target_Value,
    GroupID, 
    Sequence_No
FROM test t