Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 是否有一个函数以红移的年、月、日为单位计算datediff?_Amazon Web Services_Amazon Redshift - Fatal编程技术网

Amazon web services 是否有一个函数以红移的年、月、日为单位计算datediff?

Amazon web services 是否有一个函数以红移的年、月、日为单位计算datediff?,amazon-web-services,amazon-redshift,Amazon Web Services,Amazon Redshift,在红移时,我想以年、月和日的形式显示datediff 我尝试使用datediff和dateformat,类似于postgre,但不起作用 选择日期格式(日期差异('days','2019-01-01',当前日期),'%y yr%c mth%e dy') 我预计上述sql结果将在2年、4个月、26天内完成。日期函数在Amazon Redshift中有所不同 见: 我看不到将时间间隔(或天数)转换为人性化描述的函数。您可以使用几个DATE\u PART()命令手动对其进行编码 或者,您可以编写一个P

在红移时,我想以年、月和日的形式显示datediff

我尝试使用datediff和dateformat,类似于postgre,但不起作用

选择日期格式(日期差异('days','2019-01-01',当前日期),'%y yr%c mth%e dy')


我预计上述sql结果将在2年、4个月、26天内完成。日期函数在Amazon Redshift中有所不同

见:

我看不到将时间间隔(或天数)转换为人性化描述的函数。您可以使用几个
DATE\u PART()
命令手动对其进行编码


或者,您可以编写一个Python UDF来接受天数,然后将其格式化为友好字符串。

Postgres有一个名为AGE()的函数,但红移不支持该函数。我使用SQL UDF为这个答案创建了一个红移版本:


有一个错误,如果第一个参数的月份大于第二个,它不会返回月份部分
SELECT f_postgres_age('1994-10-04','2019-06-12')
给出
25年7天00:00:00
/*
    Postgres AGE() Function
*/
CREATE OR REPLACE FUNCTION f_postgres_age(TIMESTAMP, TIMESTAMP) 
RETURNS VARCHAR(64) 
STABLE AS $$
-- Input: '1994-04-04 20:10:52', '2018-09-24 11:31:05' -- Output: 24 years 5 mons 19 days 15:20:13
-- Input: '1994-10-04 20:10:52', '2019-06-12 11:31:05' -- Output: 24 years 8 mons 7 days 15:20:13
-- Check: SELECT '1994-10-04 20:10:52'::TIMESTAMP 
--               + INTERVAL '24 years' + INTERVAL '8 months' + INTERVAL '7 days'
--               + INTERVAL '15 hours' + INTERVAL '20 minutes' + INTERVAL '13 seconds';
-- =>     2019-06-12 11:31:05
SELECT CASE WHEN DATEDIFF(year, DATE_TRUNC('year', $1)
                              , DATE_TRUNC('year', CASE WHEN DATEPART(month, $1) > DATEPART(month, $2)
                                                        THEN $2 - INTERVAL '1 Year' ELSE $2 END)) > 0
            THEN DATEDIFF(year, DATE_TRUNC('year', $1)
                              , DATE_TRUNC('year', CASE WHEN DATEPART(month, $1) > DATEPART(month, $2)
                                                        THEN $2 - INTERVAL '1 Year' ELSE $2 END)) || ' years '
       ELSE '' END
    || CASE WHEN ABS(  DATEDIFF(month, DATE_TRUNC('month', $1), DATE_TRUNC('month', $2))
                     - DATEDIFF(month, DATE_TRUNC('year', $1)
                                     , DATE_TRUNC('year', CASE WHEN DATEPART(month, $1) > DATEPART(month, $2)
                                                               THEN $2 - INTERVAL '1 Year' ELSE $2 END))) > 0
            THEN DATEDIFF(month, DATE_TRUNC('month', $1), DATE_TRUNC('month', $2))
               - DATEDIFF(month, DATE_TRUNC('year', $1)
                               , DATE_TRUNC('year', CASE WHEN DATEPART(month, $1) > DATEPART(month, $2)
                                                         THEN $2 - INTERVAL '1 Year' ELSE $2 END)) || ' mons '
       ELSE '' END
    || CASE WHEN ABS( DATEDIFF(day, DATE_TRUNC('day', $1)+1, DATE_TRUNC('day', $2)) 
                    - DATEDIFF(day, DATE_TRUNC('month', $1), DATE_TRUNC('month', $2))) > 0
            THEN DATEDIFF(day, DATE_TRUNC('day', $1)+1, DATE_TRUNC('day', $2))
               - DATEDIFF(day, DATE_TRUNC('month', $1), DATE_TRUNC('month', $2)) || ' days '
       ELSE '' END
    || TO_CHAR((TIMESTAMP 'epoch' 
                + ( DATEDIFF(second, $1, DATE_TRUNC('day', $1)+1 )
                  + DATEDIFF(second, DATE_TRUNC('day', $2), $2) )
                * INTERVAL '1 Second '),'HH24:MI:SS') age
$$ LANGUAGE SQL
;