我如何使用 SUM() OVER()

编程入门 行业动态 更新时间:2024-10-09 16:31:45
本文介绍了我如何使用 SUM() OVER()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法理解这段代码的错误

I can't understand this code's bug

ID AccountID Quantity 1 1 10 Sum = 10 2 1 5 = 10 + 5 = 15 3 1 2 = 10 + 5 + 2 = 17 4 2 7 = 7 5 2 3 = 7 + 3 = 10 SELECT ID, AccountID, Quantity, SUM(Quantity) OVER (PARTITION BY AccountID ) AS TopBorcT, FROM tCariH

推荐答案

似乎您希望查询返回运行总数,但它必须为 AccountID 的两个分区提供相同的值.

Seems like you expected the query to return running totals, but it must have given you the same values for both partitions of AccountID.

要使用SUM() OVER()获得运行总数,您需要在PARTITION BY ...之后添加一个ORDER BY子句,像这样:

To obtain running totals with SUM() OVER (), you need to add an ORDER BY sub-clause after PARTITION BY …, like this:

SUM(Quantity) OVER (PARTITION BY AccountID ORDER BY ID)

但请记住,并非所有数据库系统都支持窗口聚合函数的 OVER 子句中的 ORDER BY.(例如,SQL Server 直到最新版本 SQL Server 2012 才支持它.)

But remember, not all database systems support ORDER BY in the OVER clause of a window aggregate function. (For instance, SQL Server didn't support it until the latest version, SQL Server 2012.)

更多推荐

我如何使用 SUM() OVER()

本文发布于:2023-06-06 00:05:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/529899.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   SUM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!