gpt4 book ai didi

mysql - 在 SQL 中汇总一列

转载 作者:行者123 更新时间:2023-11-29 03:43:15 26 4
gpt4 key购买 nike

我正在尝试在 phpmyadmin 中运行一个 SQL 查询,它将对多行进行总计并将该总计插入到单元格中。

文件按日期排序,然后是其他一些细节。

我只是很难找到它的语法。

基本上我有一个名为“Points”的列,另一个名为“Total_Points”

它需要看起来像这样:

+--------+--------------+
| Points | Total Points |
+--------+--------------+
| 10 | 10 |
| 10 | 20 |
| 10 | 30 |
| 10 | 40 |
+--------+--------------+

等等等等。
似乎必须有某种东西可以做到这一点,而我只是想念它

最佳答案

对于运行总和,您可以像这样使用“窗口函数”:

create table tbl(ord int, points int);
insert into tbl values(1, 10),(2, 10), (3, 10), (4, 10);

select
*,
sum(points) over w total_points
from tbl
window w as (order by ord);

ord | points | total_points
-----+--------+--------------
1 | 10 | 10
2 | 10 | 20
3 | 10 | 30
4 | 10 | 40

我将您的 ORDER BY 条件“聚合”为 ord 列。您当然可以更换它。

除此之外:您没有指定具体的数据库供应商。我的示例在 PostgreSQL 上运行。其他 SQL 方言可能略有不同。

关于mysql - 在 SQL 中汇总一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10486950/

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