简体   繁体   中英

How to sum up the sums of grouped results with SQL by NOT altering the structure?

I've got a column of names [name]), a column of integer values that I wanna sum up ([Values]) and another column of integer values ([Day]). I want to sum up the values grouped by name for each day. So for example if there is a name "Chris" with value 4 on day 1 and there is another entry "Chris" with value 2 on day 3, I want to show the sum of chris on day 1 (4) and on day 2 (4+2=6).

As in the example above ("chris") I wanna sum them up, showing the sum for each name on each day (the sum from day 1 until day x):

select name, day,
sum(value) over (partition by name order by day) total
from tablename 

This works BUT: I actually need to keep the structure of the table since there is actually a further column with names (name2) I need to follow the original sequence of the data because each name is uniquely assigned to another name in another column of the same row. Then, In every row the names both are assigned to a specific value of this row. Later, I want to calculate with each pair of names respectively their values (sum of values) further results. Does anybody know how to solve this problem?

EDIT:

桌子

Sorry but I was not able to insert a table properly so here is an image showing that I want to create the red marked values based on my values in the 5 columns on the left.

From what you describe, you just want to add name2 to the partition by clause:

select t.*,
       sum(value) over (partition by name, name2 order by day) as total
from tablename  t;

From the image you linked to from your request it seems you want running totals on order by day, value :

select 
  t.*
  sum(value) over (order by day, value) as sumvalue,
  sum(value2) over (order by day, value) as sumvalue2,
  sum(value) over (order by day, value) * sum(value2) over (order by day, value) as result
from tablename t
order by day, value;

With sum() window function and CTE s:

with 
  ctable as (
    select t.*, row_number() over(order by (select null)) rn
    from tablename t
  ),
  cte as (  
    select c.name, c.day, c.value, c.name2, c.value2,
    sum(c.value) over(order by rn) SumValue,
    sum(c.value2) over(order by rn) SumValue2
    from ctable c
  )
select c.*, SumValue + SumValue2 Result
from cte c

See the demo .
Results:

> name  | day | value | name2 | value2 | SumValue | SumValue2 | Result
> :---- | --: | ----: | :---- | -----: | -------: | --------: | -----:
> Chris |   1 |     2 | Paul  |      5 |        2 |         5 |      7
> Alice |   1 |     5 | Ken   |      4 |        7 |         9 |     16
> Paul  |   2 |     8 | Alice |      1 |       15 |        10 |     25
> Ken   |   2 |    11 | Chris |      2 |       26 |        12 |     38
> Alice |   3 |    14 | Ken   |      3 |       40 |        15 |     55
> Chris |   3 |    17 | Paul  |      0 |       57 |        15 |     72

I used SumValue + SumValue2 to calculate the result because in the question you say: "sum of values" . But in the expected results you use the multiplication symbol * . If you don't want the sum but the product then change accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM