带 sum 的窗口函数返回过大的数字

发布于 2025-01-12 10:40:21 字数 784 浏览 0 评论 0原文

我很困惑为什么一个简单的求和窗口函数返回以下输出,请注意“运行总计”值:

在此处输入图像描述

我的查询是:

select month,
       "total sales", 
       sum("total sales") over (order by "month number" asc) as "running total"
from
(
    select to_char(date, 'Month') as month,
           sum(sale_price) as "total sales",
           extract(MONTH from date) as "month number"
    from sales
    group by month, "month number"
) as sub_sales
order by "month number" asc;

我的输入数据如下:

在此处输入图像描述

我希望这样的结果是“运行总计”列返回 sale_price 的累积总和。

I'm confused about why a simple sum window function is returning the below output, note the "running total" value:

enter image description here

My query is:

select month,
       "total sales", 
       sum("total sales") over (order by "month number" asc) as "running total"
from
(
    select to_char(date, 'Month') as month,
           sum(sale_price) as "total sales",
           extract(MONTH from date) as "month number"
    from sales
    group by month, "month number"
) as sub_sales
order by "month number" asc;

My input data is the following:

enter image description here

I would expect this to work out such that the "running total" column returns a cumulative sum of sale_price.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里的微风 2025-01-19 10:40:21

这解决了这个问题,基于 @AdrianKlaver 评论:

select month,
       "total sales", 
       sum("total sales") over (order by "month number" asc)::int as "running total"
from
(
    select to_char(date, 'Month') as month,
           sum(sale_price) as "total sales",
           extract(MONTH from date) as "month number"
    from sales
    group by month, "month number"
) as sub_sales
order by "month number" asc;

但不确定为什么运行总计会以这种方式格式化?

This solved the issue, based on @AdrianKlaver comment:

select month,
       "total sales", 
       sum("total sales") over (order by "month number" asc)::int as "running total"
from
(
    select to_char(date, 'Month') as month,
           sum(sale_price) as "total sales",
           extract(MONTH from date) as "month number"
    from sales
    group by month, "month number"
) as sub_sales
order by "month number" asc;

Not sure why the running total would be formatted that way, though?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文