【问题标题】:how to convert unixtime stamp in date format in postgresql?如何在 postgresql 中以日期格式转换 unix 时间戳?
【发布时间】:2019-05-21 09:04:32
【问题描述】:

我正在尝试将列 unixtime 数据转换为 'YYYY-MM-DD' 格式,但没有得到它。我的列的数据类型是数字。

我尝试了以下查询,但出现错误:

select to_timestamp(starttime,'YYYY-MM-DD') , count(*) from cdrs_052019 group by 1

查询执行失败

原因:SQL 错误 [42883]:错误:函数 to_timestamp(numeric, 未知)不存在 提示:没有函数匹配给定的名称和 参数类型。您可能需要添加显式类型转换。位置: 8

|starttime |endtime   |duration|duration_min|
|----------|----------|--------|------------|
|155694960 |155694960 |0       |            |
|155694960 |155694960 |0       |            |
|155694960 |155694960 |0       |            |
|155694960 |155694960 |0       |            |
|155694960 |155694960 |0       |            |
|155694960 |155694960 |0       |            |
|155694960 |155694960 |0       |            |
|155694960 |155694960 |0       |            |
|155694960 |155694960 |0       |            |
|155694960 |155694959 |1       |            |       

需要以下格式的结果:

starttime | count(*)

2019-05-01    56666
2019-05-02    77777
2019-05-03    69495
2019-05-04    4447

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    好的,你可以通过::date将时间戳转换为日期类型,示例如下:

    postgres=# select to_timestamp(1503504000);
          to_timestamp      
    ------------------------
     2017-08-24 00:00:00+08
    (1 row)
    
    postgres=# select to_timestamp(1503504000)::date;
     to_timestamp 
    --------------
     2017-08-24
    (1 row)
    

    您想要的最终 SQL:

    select 
        to_timestamp(starttime)::date as the_date, 
        count(1) 
    from 
        cdrs_052019 
    group by 
        to_timestamp(starttime)::date 
    order by 
        the_date
    

    【讨论】:

      猜你喜欢
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 2014-01-25
      相关资源
      最近更新 更多