On Oct 27, 8:03 am, Mike Conley <[email protected]> wrote:
> On Mon, Oct 26, 2009 at 10:09 PM, Bobby Impollonia <[email protected]> wrote:
>
> Let's say you have 2 mapped classes
> class Stuff1(Base):
>      --- etc. ----
> class Stuff2(Base)
>      --- etc. ----
>
> You can do something like this
>
> q1 = session.query(Stuff1.columnA,
> Stuff1.columnB, literal_column("'S1'").label('source'))
> q2 = session.query(Stuff2.columnA,
> Stuff2.columnB, literal_column("'S2'").label('source'))
> subq = session.query().from_statement(union_all(q1, q2)).subquery()
> qry = session.query(subq)
>
> Note: just in case your font makes the quotes hard to read,
>            "'S1'" is double-quote,single-quote,S,1,single-quote,double-quote


Thank you for the help.... I've been playing with the code, but have
been unable to make it work.

Firstly, I'm not really clear on ideas behind, subquery vs. a regular
query, but I did figure out to change:

"union_all(q1, q2)" to "union_all(q1.statement, q2.statement)"

It seems to return results, but now I need to to do more things:

1) Sort the results by "time_stamp" (newest first)
2) Optionally, filter by specific "user_id"

For #1 tried doing q = DBSession.query(subq).order_by(desc
('time_stamp')), and while it seems to work, I don't understand the
SQL it generates:

SELECT anon_1.type AS anon_1_type,  anon_1.time AS anon_1_time
[......]
FROM ([....]) AS anon_1 ORDER BY time_stamp DESC

Why "AS anon_1 ORDER BY time_stamp DESC", and not "AS anon_1 ORDER BY
anon_1_time_stamp DESC"?

For #2, I am not sure where to best put the filter clause.  In q1/q2?
In subq?

Thank you,
:)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to