gpt4 book ai didi

postgresql - 两个表之间的 Postgresql 中的 Sumif

转载 作者:行者123 更新时间:2023-11-29 12:53:01 26 4
gpt4 key购买 nike

这是我的两个示例表。

表“中断”(列格式为文本、时间戳、时间戳)

+-------------------+----------------+----------------+
| outage_request_id | actual_start | actual_end |
+-------------------+----------------+----------------+
| 1-07697685 | 4/8/2015 4:48 | 4/8/2015 9:02 |
| 1-07223444 | 7/17/2015 4:24 | 8/01/2015 9:23 |
| 1-07223450 | 2/13/2015 4:24 | 4/29/2015 1:03 |
| 1-07223669 | 4/28/2017 9:20 | 4/30/2017 6:58 |
| 1-08985319 | 8/24/2015 3:18 | 8/24/2015 8:27 |
+-------------------+----------------+----------------+

和第二个表“价格”(列格式是数字,时间戳)

+-------+---------------+
| price | stamp |
+-------+---------------+
| -2.31 | 2/1/2018 3:00 |
| -2.35 | 2/1/2018 4:00 |
| -1.77 | 2/1/2018 5:00 |
| -2.96 | 2/1/2018 6:00 |
| -5.14 | 2/1/2018 7:00 |
+-------+---------------+

我的目标:对每个 outage_request_id 的开始时间和停止时间之间的价格求和。

我不知道如何正确加入表格并获得这些中断时间戳范围内的价格总和。

最佳答案

我不能保证这是有效的(事实上,对于非常大的表,我非常有信心它不是),但这在理论上应该能满足您的需求:

select
o.outage_request_id, o.actual_start, o.actual_end,
sum (p.price) as total_price
from
outage o
left join prices p on
p.stamp between o.actual_start and o.actual_end
group by
o.outage_request_id, o.actual_start, o.actual_end

关于postgresql - 两个表之间的 Postgresql 中的 Sumif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49681804/

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