Presto SQL / Athena: select between times across different days

让人想犯罪 __ 提交于 2021-02-11 14:21:37

问题


I have a database that contains a series of events and their timestamp.

I find myself needing to select all events that happen between 11:00 and 11:10 and 21:00 and 21:05, for all days.

So what I would do is I extract from timestamp the hour and the minute, and:

SELECT *
 WHERE (hour = 11 AND minute <= 10)
    OR (hour = 21 AND minute <= 05)

However, I was wondering if there's a simpler / less verbose way to do this, such as when you query between dates:

SELECT *
 WHERE date BETWEEN '2020-07-01' AND '2020-07-05'

I read here that this is doable in SQLite, I was wondering if it's possible to be done in presto as well. I've looked at the docs but couldn't find an analogue function that does what time() does in SQLite.


回答1:


You could use date formatting functions, e.g. date_format, then string comparisons:

select *
from mytable
where 
       date_format(mydate, '%H:%i') between '11:00' and '11:09'
    or date_format(mydate, '%H:%i') between '21:00' and '21:04'
    

Note that I substracted one minute from the upper bound, since I assume you don't want to include the last minute. between '11:00' and '11:09' gives you everything from 11:00:00 to 11:09:59.



来源:https://stackoverflow.com/questions/62958313/presto-sql-athena-select-between-times-across-different-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!