如何在时间戳中获得min-max

liwlm1x9  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(486)

我有表数据,需要得到一个结果集,其中包含id的max(timestamp)行
我不知道如何获取每个id和between参数的max(timestamp)行。

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

结果是:

我的结果集应该会打卡 2018-08-02 02:05:00 谁能成为我的英雄?:)谢谢你

j2datikz

j2datikz1#

您也在聚合列上执行分组。请尝试以下代码:

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

相关问题