简体   繁体   中英

Django MySQL Query - Select DateTime 60 minutes Larger Then Previous Row?

I have a model that has datetime filed called "start_time".

Lets say I do basic select query:

results = MyModel.objects.filter(start_time=somedate).order_by('start_time')

I will get something like:

2015-07-10 17:15:00
2015-07-10 19:15:00
2015-07-10 19:45:00
2015-07-10 21:15:00
2015-07-10 21:45:00

My goal is to exclude all rows that are not at least 60 minutes larger then the previous row so I would get this result:

2015-07-10 17:15:00
2015-07-10 19:15:00
2015-07-10 21:15:00

Any suggestions?

The corresponding sql query might look like this:

SELECT * FROM Table
WHERE 
  start_time > somedate
GROUP BY 
  DATE(start_time), 
  HOUR(start_time)

I'm not sure if django ORM supports this kind of GROUP BY . If not you may try raw sql.

Edit: The 60 minute or 1 hour is covered up by grouping it by date-hour.It then takes the first element of each group. If you are talking about 90 minutes or anything that doesn't fit into datetime field then I'm afraid , this approach will not work. The filtering should be done manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM