gpt4 book ai didi

sql - 将每小时价格历史记录合并到每日

转载 作者:行者123 更新时间:2023-11-29 14:06:53 25 4
gpt4 key购买 nike

数据库看起来像:

ID | volume | timestamp (timestamp without time zone)
1 | 300 | 2015-05-27 00:
1 | 250 | 2015-05-28 00:
2 | 13 | 2015-05-25 00:
1 | 500 | 2015-06-28 22:
1 | 100 | 2015-06-28 23:
2 | 11 | 2015-06-28 21:
2 | 15 | 2015-06-28 23:

是否有任何方法可以将早于 1 个月的每小时价格历史合并到每日并将它们放回表格中?这意味着将每小时记录合并为 1 条记录,总交易量和时间戳为 00 小时(我的意思是只有一天,2013 年 8 月 15 日 00:00:00)。

所以,想要的结果:

ID | volume | timestamp
1 | 300 | 2015-05-27 00:
1 | 250 | 2015-05-28 00:
2 | 13 | 2015-05-25 00:
1 | 600 | 2015-06-28 00:
2 | 26 | 2015-06-28 00:

最佳答案

看起来像一个简单的基于没有时间的日期分组:

select id,
sum(volume) as volume,
timestamp::date as timestamp
from the_table
group by id, timestamp::date
order by id, timestamp::date;

timestamp::date 会将名为 timestamp 的列(顺便说一句,这是一个可怕的列名称)转换为 date 从而删除时间戳(数据类型)的时间部分。

timestamp::date 是特定于 Postgres 的。 ANSI SQL 等效项是 cast(timestamp as date)(我有没有提到 timestamp 是列的糟糕名称?)

关于sql - 将每小时价格历史记录合并到每日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31684933/

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