I have a situation where I can have an arbitrary number of subqueries that need to be joined on the last step, except if the number of queries, n, is 1.
For example, for n = 1, suppose I have a complex query set to the variable A[1] The final submitted query would then look like: query = db.session.query(label('sid', distinct(A[1].c.patient_sid))) Easy enough! Now, suppose, I have two complex queries, A[1] and A[2] that are then joined as such: query = db.session.query(label('sid', distinct(A[1].c.patient_sid))). \ join(A[2],A[2].c.patient_sid==a[1].c.patient_sid) Not too bad... Now, I have an arbitrary number of complex queries, A[1]...A[n] that need to be joined: query = db.session.query(label('sid', distinct(A[1].c.patient_sid))). \ join(A[2],A[2].c.patient_sid==a[1].c.patient_sid). \ .... join(A[n],A[n].c.patient_sid==a[1].c.patient_sid) The above works fine, when I have conditionals based on the number n of queries, e.g., if (n == 1): query = db.session.query(label('sid', distinct(a[1].c.patient_sid))) if (n == 2): query = db.session.query(label('sid', distinct(a[1].c.patient_sid))). \ join(a[2],a[2].c.patient_sid==a[1].c.patient_sid) if (n == 3): query = db.session.query(label('sid', distinct(a[1].c.patient_sid))). \ join(a[2],a[2].c.patient_sid==a[1].c.patient_sid). \ join(a[3],a[3].c.patient_sid==a[1].c.patient_sid) etc., but since I can have an arbitrary number of these queries that need to be joined, not only is use of conditionals to set up the correct form of my join inefficient, it is highly redundant and would be a huge mess. Thus, I am wondering if it is possible to construct my join somehow, like: if (n == 1): query = 'db.session.query(label('sid', distinct(a[1].c.patient_sid)))' elif (n > 1): query = 'db.session.query(label('sid', distinct(a[1].c.patient_sid)))' for i in range (0,n) query += '.join(A[i],A[i].c.patient_sid==a[1].c.patient_sid)' eval(query) I've tried all sorts of crazy things, but cannot get the general case to work. Thanks in advance! Greg-- -- Greg M. Silverman Senior Developer Analyst Cardiovascular Informatics <http://www.med.umn.edu/cardiology/> University of Minnesota -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.