Hello,

I need to do double outer join on following 2 tables A and B to get 
presented result using ORM query or SQL expressions.

Table B should be outer joined twice to get joined 2 result sets 
(distinguished by c_id) that are for the same A records. Outer join is used 
to get NULLs where B results are missing either in first (c_id = 66) or 
second (c_id = 70) outer join.

A table:

id
--
1
2
3
4

B table:

id | a_id | c_id
---+------+------
1  |  1   | 66
2  |  2   | 66
3  |  3   | 70
4  |  4   | 66
5  |  4   | 70

Query result should be:

a_id | b1_id (66) | b2_id (70)
-----+------------+-----------
 1   |  1         |  NULL
 2   |  2         |  NULL
 3   |  NULL      |  3
 4   |  4         |  5

I got to the point where proper raw SQL query looks as follows:

SELECT 
  A.id AS a_id,
  B_1.id AS b1_id, 
  B_2.id AS b2_id,  
FROM 
  A
LEFT OUTER JOIN   B AS B_1  ON A.id = B_1.a_id AND B_1.c_id = 66
LEFT OUTER JOIN   B AS B_2  ON A.id = B_2.a_id AND B_2.c_id = 70
WHERE
  B_1.id is not NULL or 
  B_2.id is not NULL;

Now, do you know how to get this coded either in ORM query or SA SQL 
expressions?

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to