Postgresql中的日期间隔未显示正确结果

时间:2017-04-17 19:35:46

标签: date amazon-redshift intervals postgresql-8.0

运行此查询时:

SELECT  start_date,
        end_date,
        extract(epoch from end_date::timestamp - start_date::timestamp)/(24*60*60) as total,
        extract(epoch from end_date::timestamp - GETDATE()::timestamp)/(24*60*60) as left

FROM    app_data.content_cards

我得到这样一张桌子:

+---------------------+---------------------+-------+-------+
| start_date          | end_date            | total | left  |
+---------------------+---------------------+-------+-------+
| 2017-03-30 22:00:00 | 2017-04-07 22:00:00 | 8     | -9.89 |
+---------------------+---------------------+-------+-------+
| 2017-04-09 22:00:00 | 2017-04-11 22:00:00 | 2     | -5.89 |
+---------------------+---------------------+-------+-------+
| 2017-04-03 22:00:00 | 2017-04-11 22:00:00 | 8     | -5.89 |
+---------------------+---------------------+-------+-------+
| 2017-03-30 22:00:00 | 2017-04-18 22:00:00 | 19    | 1.11  |
+---------------------+---------------------+-------+-------+
| 2017-04-09 22:00:00 | 2017-04-15 22:00:00 | 6     | -1.89 |
+---------------------+---------------------+-------+-------+
| 2017-04-02 22:00:00 | 2017-05-20 22:00:00 | 78    | 63.11 |
+---------------------+---------------------+-------+-------+
| 2017-04-23 22:00:00 | 2017-04-29 22:00:00 | 6     | 12.11 |
+---------------------+---------------------+-------+-------+
| 2017-04-02 22:00:00 | 2017-05-20 22:00:00 | 78    | 63.11 |
+---------------------+---------------------+-------+-------+

但是如果我使用这个查询:

SELECT  start_date,
        end_date,
        end_date - start_date as total,
        end_date - GETDATE() as days

FROM    app_data.content_cards

我得到这样一张桌子:

+--------------------+-------------------+-------+-----------------+
| start_date         | end_date          | total | left            |
+--------------------+-------------------+-------+-----------------+
|2017-03-30 22:00:00 |2017-04-07 22:00:00|8 days |-9 days -21:21:47|
+--------------------+-------------------+-------+-----------------+
|2017-04-09 22:00:00 |2017-04-11 22:00:00|2 days |-5 days -21:21:47|
+--------------------+-------------------+-------+-----------------+
| 2017-04-03 22:00:00|2017-04-11 22:00:00|8 days |-5 days -21:21:47|
+--------------------+-------------------+-------+-----------------+
| 2017-03-30 22:00:00|2017-04-18 22:00:00|19 days|1 day 02:38:13   |
+--------------------+-------------------+-------+-----------------+
| 2017-04-09 22:00:00|2017-04-15 22:00:00|6 days |-1 days -21:21:47|
+--------------------+-------------------+-------+-----------------+
| 2017-04-02 22:00:00|2017-05-20 22:00:00|48 days|33 days 02:38:13 |
+--------------------+-------------------+-------+-----------------+
| 2017-04-23 22:00:00|2017-04-29 22:00:00|6 days |12 days 02:38:13 |
+--------------------+-------------------+-------+-----------------+
| 2017-04-02 22:00:00|2017-05-20 22:00:00|48 days|33 days 02:38:13 |
+--------------------+-------------------+-------+-----------------+

请注意第1行和第1行中的不同结果3从底部开始。正确应该是最后一个查询返回的那个。

我需要的是将结果以天为单位并以小数点后两位(如第一张表中所示),但数据正确(如第二个)。另外,我需要数据在结果中没有'days',只需要两位小数的值。

我怎样才能做到这一点?

我正在使用:i686-pc-linux-gnu上的版本PostgreSQL 8.0.2,由GCC gcc(GCC)3.4.2 20041017(Red Hat 3.4.2-6.fc3)编译,Redshift 1.0.1232

1 个答案:

答案 0 :(得分:1)

此查询应该适合您。

select  start_date,
        end_date,
        (date_part(epoch,end_date)::float- date_part(epoch,start_date)::float)/(24*60*60) as total,
        (date_part(epoch,end_date)::float- date_part(epoch,sysdate)::float)/(24*60*60) as total left        
from    app_data.content_cards

OR
您也可以使用提取功能。

SELECT  start_date, 
        end_date, 
        (extract(epoch from end_date)::float - extract(epoch from start_date)::float)/(24*60*60) as total,
        (extract(epoch from end_date)::float - extract(epoch from sysdate)::float)/(24*60*60) as left
FROM    app_data.content_cards

如果我做了一个错误的假设请发表评论,我会重新调整我的答案。