【问题标题】:difference in postgres and redshift querypostgres 和 redshift 查询的区别
【发布时间】:2016-09-08 18:15:14
【问题描述】:

1> 在 AWS RedShift 中运行良好:

rep=# \d repdaily
                   Table "prod.repdaily"
          Column           |  Type   |     Modifiers      
---------------------------+---------+--------------------
 timestamp                 | integer | not null default 0


rep=# SELECT distinct trunc(TIMESTAMP 'epoch' + ((floor(timestamp/86400))*86400) *INTERVAL '1 second') as Date_New FROM repdaily limit 1;
  date_new  
------------
 2016-06-26
(1 row)

rep=# select version();
                                                         version                                                          
--------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.1096
(1 row)

rep=# 

2>我尝试在 9.5.4 和 8.0.2 中运行相同的查询:

我不知道如何使它通用,以便我们可以在任何地方运行它。

rep=# \d repdaily
                   Table "prod.repdaily"
          Column           |  Type   |     Modifiers      
---------------------------+---------+--------------------
 timestamp                 | integer | not null default 0

rep=# 
rep=# SELECT distinct trunc(TIMESTAMP 'epoch' + ((floor(timestamp/86400))*86400) *INTERVAL '1 second') as Date_New FROM repdaily limit 1;
ERROR:  function trunc(timestamp without time zone) does not exist
LINE 1: SELECT distinct trunc(TIMESTAMP 'epoch' + ((floor(timestamp/...
                        ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
rep=#

rep=# select version();
                                                 version                                                  
----------------------------------------------------------------------------------------------------------
 PostgreSQL 9.5.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17), 64-bit
(1 row)

rep=# 

rep=# select version();
                                                  version                                                  
-----------------------------------------------------------------------------------------------------------
 PostgreSQL 8.0.2 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17)
(1 row)

rep=# 

【问题讨论】:

  • 在 Postgres 中是 date_trunc(),而不仅仅是 trunc()

标签: postgresql amazon-redshift


【解决方案1】:

PostgreSQL 没有原生的 trunc(timestamp) 函数。查看 RedShift 的trunc(timestamp) function 可以看到:

该函数还可以从时间戳返回日期。 对于时间戳,TRUNC 返回一个日期。

所以 PostgreSQL 的等价物是一个简单的转换为 DATE 类型:

SELECT distinct
    CAST(TIMESTAMP 'epoch' + ((floor(timestamp/86400))*86400) *INTERVAL '1 second' AS date) as Date_New
FROM repdaily limit 1;

如果你愿意,你可以在 PostgreSQL 中简单地创建这个 trunc(timestamp) 函数:

CREATE OR REPLACE FUNCTION trunc(timestamp)
RETURNS date
LANGUAGE SQL
IMMUTABLE AS $$
    SELECT $1::date;
$$;

现在您的原始查询也可以在 PostgreSQL 中使用。

【讨论】:

  • 对不起,我正在放长假,无法回复。 MatheusOl @ 如果我没记错的话,我得到的最佳答案而且效果很好。非常感谢您的帮助!!!!
猜你喜欢
  • 2015-03-22
  • 2016-08-11
  • 2014-12-10
  • 1970-01-01
  • 2023-03-24
  • 2018-06-08
  • 2012-09-09
  • 2013-06-20
  • 2019-11-21
相关资源
最近更新 更多