zoukankan      html  css  js  c++  java
  • postgresql 教程

    获取当前时间戳-秒

    使用PostgreSQL获取当前系统时间戳。众所周知,在MySQL中是这样的:

    select UNIX_TIMESTAMP(NOW())

    新纪元时间 Epoch函数 是以 1970-01-01 00:00:00 UTC 为标准的时间,将目标时间与 1970-01-01 00:00:00
    时间的差值以秒来计算 ,单位是秒

    extract函数格式:
    extract (field from source)
    extract函数是从日期或者时间数值里面抽取子域,比如年、月、日等。source必须是timestamp、time、interval类型的值表达式。field是一个标识符或字符串,是从源数据中的抽取的域。

    (1)精确到秒

    select floor(extract(epoch from now())); 结果:"1574826646"

    (2)精确到秒的小数

    select extract(epoch from now());结果:"1574826646.79929"

    (3)精确到毫秒:

    select floor(extract(epoch from((current_timestamp - timestamp '1970-01-01 00:00:00')*1000)));

    时间戳转时间

    SELECT TO_TIMESTAMP(extract(epoch from now()))

    获取系统时间函数

    获取当前时间

    select now();

    结果:2021-08-11 17:11:30.652965+08

    current_timestamp 同 now() 函数等效。

    select current_timestamp;

    获取当前时间不带年月日

    select current_time;

    结果:17:13:36.524366+08

    获取当前日期

    select current_date;

    时间的计算

    两年后

    select now() + interval '2 years';

    结果:2023-08-11 17:14:54.27425+08

    一个月后

    select now() + interval '1 month';

    三周前

    select now() - interval '3 week';

    1天前

    select now() - interval '1 day';

    十分钟后

    select now() + '10 min';

    计算两个时间差

    使用 age(timestamp, timestamp)

    select age(now(), timestamp '1989-02-05');

    当前时间可以省略

    select age(timestamp '2007-09-15');  

    timestamp是获取指定时间的年月日 时分秒格式

    select timestamp '1989-02-05';

    时间差秒数:

    语法:

    extract(epoch FROM (timeatmp1-timestamp2))

    例如:

    SELECT
    extract(epoch FROM (now() - (to_timestamp(xapp_recently_login_in_time,'yyyy-MM-dd hh24:mi:ss')) ))as gap_time,
    to_timestamp(xapp_recently_login_in_time,'yyyy-MM-dd hh24:mi:ss')as new_xapp_recently_login_in_time
    ,* FROM dws_trfc_soul_seller_recently_login_intime
    where buyer_id is not null

    时间字段的截取

    在开发过程中,经常要取日期的年,月,日,小时等值,PostgreSQL 提供一个非常便利的EXTRACT函数。

    EXTRACT(field FROM source)

    field 表示取的时间对象,source 表示取的日期来源,类型为 timestamp、time 或 interval。

    取年份

    select extract(year from now());

    取月份

    select extract(MONTH from now());

    取天

    select extract(day from timestamp '2013-04-13');

    substr函数

    substr(ftd_first_login_time,1,13)

    查看现在距1970-01-01 00:00:00 UTC 的秒数

    将当前时间转成秒类型,然后截取秒数

    select extract(epoch from now());

    date_part函数

    date_part函数是仿照在传统的Ingres函数。等效于 SQL 标准函数extract:

    date_part('field',source)

    SELECT date_part('hour', INTERVAL '4 hours 3 minutes');

    Result:4

    时间间隔

    时间间隔 interval [fields][(p)]

    select interval '1 year 2 months 3 days 4 hours 5 minutes 6 seconds';

    结果:

    1 year 2 mons 3 days 04:05:06

    date_part(‘epoch’, end_time - begin_time)

    将时间差转换为秒,然后转换数据类型为数字类型除以60,得到分钟数

    select date_part('epoch', now() - '2021-08-11 17:00:00')::NUMERIC / 60

    时区

    with和without time zone两者有什么区别

    1.区别

    1)名字上看一个是带时区的,另一个是不带时区的,查出来的时间是一样的,只是一个带时区标志,一个不带而已,时区的基准是格林威治时间UTC。
    2)这对于数据的显示上来说,区别就是时间数据的末尾带不带时区标志,即+/-时区,比如中国(prc),时区是东八区,带时区标志的话就是+08。

    +08:表示 时区与全球统一时间 UTC 偏移量为 8 小时

    without time zone

    不带时区

    select  now()::timestamp without time zone

    结果:2021-08-11 18:26:34.751754

    with time zone

    带时区

    select  now()::timestamp with time zone

    结果:2021-08-11 18:27:53.889618+08

    at time zone 'GMT-8'

    AT TIME ZONE 构造允许把时间戳转换成不同的时区与timezone(zonetimestamp) 函数等效

    指定分区类型

    select now()::timestamp at time zone 'GMT-8'

    结果:2021-08-11 18:28:46.212463+08

    select now()::timestamp at time zone 'HKT'

    结果为:2021-08-11 18:30:44.73845+08

    将日期转成字符串

    to_char(time,'YYYY-MM-DD hh24:mi:ss') as time1,

    to_char(time,'YYYY-MM-DD') as time2,

    to_char(time,'YYYY-MM-DD hh:mi:ss') as time3

    换行符和拼接符

    '>'||scene_type||'异常类目长总数 '||corp_cnt,E' '

    拼接使用‘||’,被拼接的是非字段而是符号或者字符,需要单引号括一下,换行符在pgsql中是“E‘ ’”,需要在换行符前加逗号

    如果是用chr(10),那么char(10)前都必须用||连接

    行转列

    case when

    select data_dt
    ,work_we_chat_id
    ,corp_code
    ,corp_name
    ,NVL(zhike_indic_name,'')as zhike_indic_name
    ,NVL(zhike_indic_value,'')as zhike_indic_value
    from (SELECT
    substr(data_dt,1,10) as data_dt,work_we_chat_id,corp_code,corp_name
    ,case when indic_code='deal_by_plt' then indic_name end as zhike_indic_name
    ,case when indic_code='deal_by_plt' then indic_value end as zhike_indic_value
    FROM edw_svc_cs_event_stat_for_test
    where length(indic_value)>0
    and indic_value>0.0
    and indic_code in ('deal_by_plt')
    order by work_we_chat_id)t

    string_agg

    例如:string_agg('>'||scene_type||'异常类目长总数 '||corp_cnt,E' ') as pinjie 

    如果scene_type有多个值,即多行记录,拼接完cnt值后会自动换行取下一个类型的值

    达到一个商家打印出多个类型值

    同行专列一个效果

  • 相关阅读:
    Asp.Net异步页处理
    iframe通过js自动调节高度
    Js 代码
    JavaScript使用面向对象的技术创建高级 Web 应用程序 (转)
    C#泛型深入浅出(转)
    Asp.Net异步页面处理(转)
    ASP.NET分页存储过程自定义用户控件(转)
    .NET Remoting程序开发入门篇(转)
    认识WebService (转)
    Java CompletableFuture 详解
  • 原文地址:https://www.cnblogs.com/T-CYP/p/15129095.html
Copyright © 2011-2022 走看看