使用过度分区滚动总计时处理重复

发布于 2025-02-04 03:30:11 字数 3356 浏览 3 评论 0原文

我试图在每个日期(从一个月的第一天到日期列值所显示的,在输入表中显示的任何日期,都可以获取滚动金额列的总计。

输出要求

  1. “团队”列的分区
  2. 在每个月的1个重新启动滚动总计

问题1
我的下面查询是否正确以获取下面输出表中显示的所需输出要求?它似乎有效,但我必须确认。

SELECT
    *,
    SUM(amount) OVER (
        PARTITION BY
            team,
            month_id
        ORDER BY
            date ASC
    ) rolling_amount_total
FROM input_table;

问题2
如何处理在输入表的前2行中显示的重复日期?每当有重复日期时,金额也是重复的。我在此处看到一个解决方案: https://stackoverflow.com/a/60115015061/6388651 没有运气删除它去删除它得到它的删除重复。我的非工作代码示例如下。

SELECT
    *,
    SUM(amount) OVER (
        PARTITION BY
            team,
            month_id
        ORDER BY
            date ASC
    ) rolling_amount_total
FROM (
    SELECT DISTINCT
        date,
        amount,
        team,
        month_id
    FROM input_table
) t

Input Table

dateamountteammonth_id
2022-04-011A2022-04
2022-04-011A2022-04
2022-04-022A2022-04
2022-05-014B2022-05
2022-05- 024B2022-05

所需的输出表

team teamteam_idrolling_amount_total
2022-04-011A2022-041
2022-04-04-022A2022-043
2022-04 3 2022-05-014B2022-2022-2022-054 2022-05 4
2022-05 5 -024B2022-058

I'm trying to get the rolling amount column totals for each date, from the 1st day of the month to whatever the date column value is, shown in the input table.

Output Requirements

  1. Partition by the 'team' column
  2. Restart rolling totals on the 1st of each month

Question 1
Is my below query correct to get my desired output requirements shown in Output Table below? It seems to work but I must confirm.

SELECT
    *,
    SUM(amount) OVER (
        PARTITION BY
            team,
            month_id
        ORDER BY
            date ASC
    ) rolling_amount_total
FROM input_table;

Question 2
How can I handle duplicate dates, shown in the first 2 rows of Input Table? Whenever there is a duplicate date the amount is a duplicate as well. I see a solution here: https://stackoverflow.com/a/60115061/6388651 but no luck getting it to remove the duplicates. My non-working code example is below.

SELECT
    *,
    SUM(amount) OVER (
        PARTITION BY
            team,
            month_id
        ORDER BY
            date ASC
    ) rolling_amount_total
FROM (
    SELECT DISTINCT
        date,
        amount,
        team,
        month_id
    FROM input_table
) t

Input Table

dateamountteammonth_id
2022-04-011A2022-04
2022-04-011A2022-04
2022-04-022A2022-04
2022-05-014B2022-05
2022-05-024B2022-05

Desired Output Table

dateamountteammonth_idRolling_Amount_Total
2022-04-011A2022-041
2022-04-022A2022-043
2022-05-014B2022-054
2022-05-024B2022-058

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

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

发布评论

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

评论(1

愁杀 2025-02-11 03:30:11

Q1。您的sum()aver()是正确的
Q2。在您的第一个查询中,从input_table 中替换,使用:

from (select date, sum(amount) as amount, team, month_id 
      from input_table 
      group by date, team, month_id
      ) as t

Q1. Your sum() over () is correct
Q2. Replace from input_table, in your first query, with :

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