Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

python - SQLAlchemy Group By With Full Child Objects

Imagine the following Media table:

| site       | show_id | time |
| ---------------------|-------|
| CNN        | 1       | 'a'   |
| ABC        | 2       | 'b'   |
| ABC        | 5       | 'c'   |
| CNN        | 3       | 'd'   |
| NBC        | 4       | 'e'   |
| NBC        | 5       | 'f'   |
--------------------------------

I would like to iterate over query results grouped by show_id and have tried this query:

listings = session.query(Media).filter(Media.site == "CNN").group_by(Media.show_id).all()

Here's how I would like to iterate over the results:

for showtimes in listings:
    for show in showtimes:
        print(show.time)

But that query doesn't give me all of the grouped child objects. What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In SQL the GROUP BY clause condenses the grouped rows into a single row, based on the grouping expressions. My guess is that you are using SQLite, or an older version of MySQL, since you are allowed to select non-aggregates without them being functionally dependent on the grouping expressions. The results contain values from an unspecified row per group in that case, which is seldom — if ever — useful.

A solution is to ORDER BY instead of GROUP BY in SQL and then group in Python based on the order expressions:

from itertools import groupby
from operator import attrgetter


listings = session.query(Media).
    filter(Media.site == "CNN").
    order_by(Media.show_id).
    all()

# Materialize the subiterators to lists
listings = [list(g) for k, g in groupby(listings, attrgetter('show_id'))]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...