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

    select extract(century from timestamp '2018-08-01 12:12:13');> 21select extract(year from timestamp '2018-08-01 12:12:13');> 2018select extract(month from timestamp '2018-08-01 12:12:13');> 8select extract(day from timestamp '2018-08-01 12:12:13');> 1select extract(hour from timestamp '2018-08-01 12:12:13');> 12select extract(min from timestamp '2018-08-01 12:12:13');> 12select 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)> 61select date_part('month','2022-05-01 12:00:00'::timestamp - '2021-03-01 00:00:00'::timestamp)> 0select date_part('hour','2022-05-01 12:00:00'::timestamp - '2021-03-01 00:00:00'::timestamp)> 12select 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_valuefrom (     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)agroup by superid> 11111     可控门店,焕新服务   2222    可控门店,焕新服务   3333    可控门店
  4. 获取以后工夫

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

    select cast(date_column as varchar)