gpt4 book ai didi

sql - 总和列以显示每一行的总数

转载 作者:行者123 更新时间:2023-12-03 16:30:02 24 4
gpt4 key购买 nike

我有一个查询,看起来像这样:

select Group, Sum(columnA) as SumColumn
FROM table
GROUP BY Group

我得到的结果看起来像这样
+-----+---------+
|Group|SumColumn|
+-----+---------+
|A |10 |
+-----+---------+
|B |20 |
+-----+---------+

如何更改/添加到此显示类似的内容?
+-----+---------+-----------+
|Group|SumColumn|TotalColumn|
+-----+---------+-----------+
|A |10 |30 |
+-----+---------+-----------+
|B |20 |30 |
+-----+---------+-----------+

最佳答案

很难看到您的数据是什么样子-但是从您发布的内容来看,这就是您想要的:

SELECT Name, 
SumColumn,
SUM(SumColumn) AS TotalColumn
FROM
(
SELECT Group as Name, SUM(columnA) AS SumColumn
FROM Table
GROUP BY Group
) T

您可能想要这个-取决于其他内容。
SELECT *,  
SUM(columnA) OVER (PARTITION BY Group ORDER BY Group) AS SumColumn,
SUM(columnA) OVER (PARTITION BY Group) AS TotalColumn
FROM TABLE

关于sql - 总和列以显示每一行的总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21167073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com