关于greenplum:Greenplum日期函数记录

6次阅读

共计 1702 个字符,预计需要花费 5 分钟才能阅读完成。

工作中常常用到 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)
正文完
 0