gpt4 book ai didi

postgresql - 在psql的一列中连接日期(时间戳)和小时(数字)列

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

我在服务器中有一个表,其中 tx_date 在时间戳中,小时在数字中是不同的列,因此我需要组合这两个列以在 sql 查询中获得一个结果。

这些是我表格的列。

tx_date            |hour|customer_obj_num|subscriber_obj_num|
-------------------|----|----------------|------------------|
2019-06-06 00:00:0 |9 |1260340723780 |1260341599747 |
2019-07-12 00:00:0 |9 |1260340917755 |1260341740838 |
2019-07-10 00:00:0 |18 |1261891161687 |1261891162660 |
2019-06-07 00:00:0 |14 |1260341590405 |1260488404515 |
2019-06-27 00:00:0 |10 |1263742570630 |1263742571046 |

需要这样的结果:

tx_date              |customer_obj_num|subscriber_obj_num|
---------------------|----------------|------------------|
2019-06-06 09:00:0 |1260340723780 |1260341599747 |
2019-07-12 09:00:0 |1260340917755 |1260341740838 |
2019-07-10 18:00:0 |1261891161687 |1261891162660 |
2019-06-07 14:00:0 |1260341590405 |1260488404515 |
2019-06-27 10:00:0 |1263742570630 |1263742571046 |

最佳答案

您可以使用 make_interval() 将小时数转换为时间间隔并将其添加到时间戳:

如果 hour 不是整数,则需要对其进行转换。

select t.tx_date + make_interval(hours => t.hour::int)
from the_table t;

如果 hour 是带小数的数值(以及那些小数)应该被视为小数小时,您可以转换为秒。

select t.tx_date + make_interval(secs => t.hour * 60 * 60)
from the_table t;

(如果 hour 不包含小数小时,那么为什么它是数字而不是整数)

在线示例:https://rextester.com/RHYA54311

关于postgresql - 在psql的一列中连接日期(时间戳)和小时(数字)列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57820180/

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