postgres和redshift查询的区别

时间:2016-09-08 09:22:30

标签: postgresql amazon-redshift

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=# 

1 个答案:

答案 0 :(得分:0)

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。