[英]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]).我有一列名称 [name])、一列我想总结的整数值 ([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).因此,例如,如果在第 1 天有一个值为 4 的名称“Chris”,并且在第 3 天有另一个值为 2 的条目“Chris”,我想显示第 1 (4) 天和第 2 天的 Chris 总和(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):正如上面的例子(“克里斯”),我想总结一下,显示每天每个名字的总和(从第 1 天到第 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.这有效但是:我实际上需要保留表的结构,因为实际上还有一个带有名称(name2)的列我需要遵循数据的原始顺序,因为每个名称都唯一地分配给另一个名称的另一列中同一行。 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.抱歉,我无法正确插入表格,因此这里的图像显示我想根据左侧 5 列中的值创建红色标记的值。
From what you describe, you just want to add name2
to the partition by
clause:根据您的描述,您只想将name2
添加到partition by
子句中:
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
:从您从请求中链接到的图像看来,您似乎希望order by day, value
按order by day, value
运行总计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:使用sum()
窗口函数和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" .我使用SumValue + SumValue2
来计算结果,因为在问题中你说: "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.如果您不想要总和但产品,则相应地更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.