Greenplum日期函数记录

工作中经常用到greenplum日期函数,特此记录,以便查阅!
  1. 查询时间戳对应的年月日时分秒

    select extract(century from timestamp '2018-08-01 12:12:13');
    > 21
    select extract(year from timestamp '2018-08-01 12:12:13');
    > 2018
    select extract(month from timestamp '2018-08-01 12:12:13');
    > 8
    select extract(day from timestamp '2018-08-01 12:12:13');
    > 1
    select extract(hour from timestamp '2018-08-01 12:12:13');
    > 12
    select extract(min from timestamp '2018-08-01 12:12:13');
    > 12
    select extract(sec from timestamp '2018-08-01 12:12:13');
    > 13
  2. 查询日期之间的天数差,date_depart参数为day时,返回两个日期之间的天数差,month 不起作用,hour则只截取两个日期的hour做差,不能用;月份差可在天数差的基础上除以30,算作基础的月份差。

    select date_part('day','2022-05-01 00:00:00'::timestamp - '2021-03-01 00:00:00'::timestamp)
    > 61
    select date_part('month','2022-05-01 12:00:00'::timestamp - '2021-03-01 00:00:00'::timestamp)
    > 0
    select date_part('hour','2022-05-01 12:00:00'::timestamp - '2021-03-01 00:00:00'::timestamp)
    > 12
    select date_part('day','2022-05-01 00:00:00'::timestamp - '2021-03-01 00:00:00'::timestamp)/30
    > 2
  3. 查询分组的字符串拼接函数
    string_agg(A,B),需配合分组使用,A是拼接字段,B是分隔符号以及字符串拼接的排序,与mysql中的group_contact()是一个作用。例如,返回订单表中不同用户的累计信息渠道,一个用户可能在一个信息渠道停留多次,需要去重,不同的信息渠道进行拼接,且针对所有用户拼接字段的顺序要相同一致:

    select  superid , string_agg(tag_value,',' order by tag_value) as tag_value
    from ( 
        select superid,tag_value 
        from (
            select 
            b.superid, 
            (case when trim(a.order_channel)='CMS' then '可控门店'
            when trim(a.order_channel)='EOS' then '焕新服务'
            when trim(a.order_channel)='E3' then order_source
            else '微商城' 
            end )as tag_value
            from cdp_order_model a
            join superid_all b on a.customer_id = b.id and b.idtype ='jiami_mobile'
            where channel='cdp_order_sale_01'
        ) c
        group by c.superid,c.tag_value
    )a
    group by superid
    > 11111     可控门店,焕新服务
       2222    可控门店,焕新服务
       3333    可控门店
  4. 获取当前时间

    select now()
    > 2022-05-20 18:47:17.706284+08
    select current_date
    > 2022-05-20
    select current_timestamp
    > 2022-05-20 18:47:17.706284+08
  5. 将日期转换为字符串

    select cast(date_column as varchar)

你可能感兴趣的:(Greenplum日期函数记录)