Aggregate in SQLite or Python

妖精的绣舞 提交于 2019-12-24 13:33:39

问题


Suppose I have a table with a article headers, author, and also a publication date, I retrieve all of the ones that are published after July 1st, - but now I want to iterate through not the article headers individually, but through sets of all the articles published on each day, what's the best and most pythonic way to build this list. Can it be done in the sqlite query?

Edit: I don't actually have a table with articles in sqlite3, but let's suppose I did. And suppose the table articles is organized with:

title TEXT, author TEXT, publisher TEXT, date DATETIME

The articles might be fetched like so:

cursor.execute("SELECT * FROM articles where date > ?", \ 
(datetime.datetime(2014, 07, 01),))

and could be grouped by (Following Holdenweb's answer below):

itertools.groupby(cursor.fetchall(), lambda x: datetime.strptime(x[3], '%Y-%m-%d %H:%M:%S.%f').day)

which will give a tuple of (day, group), and can be iterated over in the manner described below.


回答1:


A SQL query will normally only return a single set of rows as a result.

Let's say you have retrieved all the rows you want in a cursor curs, and assume that the resulting rows are each made up of (header, author, pub_date) and that you sorted the data in SQL in ascending order of pub_date.

Using itertools.groupby() it's very easy to define a key function that returns the pub_date colum (lambda r: r[2] comes to mind). You can then iterate over the groupby() result which is a series of (key_value, group) tuples where key_value will take the successive unique values of the pub_date column and group will be an iterator yielding successive rows associated with that key value. Try something like

for key_val, group in itertools.groupby(curs.fetchall(), lambda r: r[2]):
    print key_val
    for item in group:
        print "\t", item

to verify this works as you want.



来源:https://stackoverflow.com/questions/24696351/aggregate-in-sqlite-or-python

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