mercredi 1 avril 2015

How to do an equivalent of GROUP BY in python

I'm querying a SQLite database for messages inside hourly intervals, where I am interested in one entry pr userid pr hour (this is done using the GROUP BY clause). To query the database for every hourly interval takes a fairly long time, so I was thinking I would save time if I only queried the database once without any time interval, then rather manipulated the resulting array (chunking it up in hour sized chunks). But how can I do this?



for k in range(0,3000):
start = 1398942000+k*60*60
end = 1398942000+(k+1)*60*60
cur.execute('SELECT userid, unixtime, latitude, longitude FROM table WHERE unixtime > {start} AND unixtime < {end} group by userid'.format(start = start, end = end))


This is my query. Ideally it would look like this:



cur.execute('SELECT userid, unixtime latitude, longitude FROM message')


A sudo of what I think a fitting solution would be:



for k in range(0,137*24):
start = 1398942000+k*60*60
end = 1398942000+(k+1)*60*60
thisHourInterval = list[:indextoClosestUnixTimeToEnd]
list = list[indextoClosestUnixTimeToEnd:]
*Only one entry pr id in thisHourInterval*

Aucun commentaire:

Enregistrer un commentaire