I need to do double outer join on following 2 tables A and B to get presented result using SQLAlchemy ORM 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 SA ORM or SA SQL expressions?
Found the solution myself:
b1 = aliased(B)
b2 = aliased(B)
q = session.query(A.id, b1.id.label("b1_id"), b1.id.label("b2_id"))
q = q.outerjoin(b1, sqlalchemy.and_(A.id == b1.a_id, b1.c_id == 66))
q = q.outerjoin(b2, sqlalchemy.and_(A.id == b2.a_id, b2.c_id == 70))
q = q.filter(sqlalchemy.or_(b1.id != None, b2.id != None))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With