在不同条件下执行两次求和

发布于 2025-01-10 23:41:35 字数 1291 浏览 1 评论 0原文

我需要在不同条件下执行两次求和:

  • 第一个求和:必须返回每个 date1 和 id 的值之和(按 date1 和 id 分组)。
  • Second Sum:必须返回每个 date1 和 id 的值之和(按 date1 和 id 分组),没有与最小的 date2 对应的值。
IDDate1Date2
01012022-02-152022-02-15
01012022-02-152022-02-19
01032022-02-152022-02-17
01022022-02-162022-02-20
01012022-02-162022-02-22
01042022-02-162022-02-28

预期结果:

IDDate1SUM1SUM2
012022-02-150504
012022-02-160705
  • 在sum2中,对于data1=2022-02-15,删除了data2=2022-02-15对应的值,因为它是分组中data2的最小值。

  • 在 sum2 中,对于 data1 = 2022-02-16,对应于 data2=2022-02-20 的值被删除,因为它是分组中 data2 的最小值。

关于如何获得这个结果有什么建议吗?

I need to perform two Sums with different conditions:

  • First Sum: Must return the sum of values for each date1 and id (Group by date1 and id).
  • Second Sum: Must return the sum of values for each date1 and id (Group by date1 and id), without the value corresponding to the smallest date2 .
IDValueDate1Date2
01012022-02-152022-02-15
01012022-02-152022-02-19
01032022-02-152022-02-17
01022022-02-162022-02-20
01012022-02-162022-02-22
01042022-02-162022-02-28

Expected outcome:

IDDate1SUM1SUM2
012022-02-150504
012022-02-160705
  • In sum2, for data1=2022-02-15, the value corresponding to data2=2022-02-15 was removed, as it is the smallest value of data2 in the grouping.

  • In sum2 , for data1 = 2022-02-16, the value corresponding to data2=2022-02-20 was removed, as it is the smallest value of data2 in the grouping.

Any suggestions on how to get this result?

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

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

发布评论

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

评论(2

心如荒岛 2025-01-17 23:41:35

由于 partition by 并不总是能与 group by 配合良好,因此您可以使用 CTE 来实现结果:

with tmp as (
    select tv.*, first_value(value) over (partition by id, date1 order by date2) date2_value
    from table_views tv
)
select id, date1, sum(value) 
from tmp
group by id, date1
union all
select id, date1, sum(value) - min(date2_value)
from tmp
group by id, date1;

这里是db_fiddle。

我在这里使用了 min ,但这是任意的,任何其他都可以:maxavg 等。

Since partition by does not always play well with group by, you can use CTE to achieve the result:

with tmp as (
    select tv.*, first_value(value) over (partition by id, date1 order by date2) date2_value
    from table_views tv
)
select id, date1, sum(value) 
from tmp
group by id, date1
union all
select id, date1, sum(value) - min(date2_value)
from tmp
group by id, date1;

Here is the db_fiddle.

I've used min here, but that is arbitraty and any other would work: max, avg etc.

打小就很酷 2025-01-17 23:41:35

您可以尝试在子查询中使用LEAD窗口函数,它将获取每个Date1的下一个值(这将忽略每个Date1的最小值)。

然后对 value 列进行 sum LEAD 窗口函数列。

SELECT ID,
       Date1,
       SUM(Value) SUM1,
       SUM(n_val) SUM2
FROM (
    SELECT *,
           LEAD(Value) OVER(PARTITION BY ID,Date1 ORDER BY Date2) n_val
    FROM table_views 
) t1
GROUP BY ID,Date1;

sqlfiddle

You can try to use LEAD window function in subquery which will get the next value per Date1 (that will ignore the smallest per Date1).

Then do sum on value column & LEAD window function column.

SELECT ID,
       Date1,
       SUM(Value) SUM1,
       SUM(n_val) SUM2
FROM (
    SELECT *,
           LEAD(Value) OVER(PARTITION BY ID,Date1 ORDER BY Date2) n_val
    FROM table_views 
) t1
GROUP BY ID,Date1;

sqlfiddle

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