【问题标题】:how to get min max in timestamp如何在时间戳中获得最小值最大值
【发布时间】:2019-01-16 09:23:56
【问题描述】:

我有表格数据,需要获取一个结果集,其中包含 id 的 max(timestamp) 行。参数之间的时间戳 数据样本如下所示:example data

我不知道如何为每个 id 和参数之间的 max(timestamp) 获取一行。

select id,
        tgl,
         max(case when tgl::timestamp between '2018-08-01 06:00'::timestamp and '2018-08-02 06:00'::timestamp 
        then tgl::timestamp else null end) clock
        from tb_raw_pola_internal
        where id = '0023817'
        group by 1, 2
        order by 1,2 asc
        limit 1

结果是:

我的结果集应该是2018-08-02 02:05:00

谁能成为我的英雄? :) 谢谢之前

【问题讨论】:

    标签: mysql timestamp max min


    【解决方案1】:

    您也在聚合列上进行分组。试试下面的代码:

        select id,
        tgl,
         max(case when tgl::timestamp between '2018-08-01 06:00'::timestamp 
         and '2018-08-02 06:00'::timestamp 
        then tgl::timestamp else null end) clock
        from tb_raw_pola_internal
        where id = '0023817'
        group by 1;
    

    【讨论】:

      猜你喜欢
      • 2012-04-03
      • 1970-01-01
      • 2017-05-12
      • 2018-02-18
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      相关资源
      最近更新 更多