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



我在服务器中有一个表,其中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

您可以将tx_date转换为日期,然后只需向其添加小时数

Select tx_date::date + interval '1 hour'*hour from table

最新更新