サブクエリを使用して結果セットの列を表すクエリを実行しようとしています。
select id_column1, id_column2, id_column3,
(select column4 from table2 where id in (id_column1, id_column2, id_column3)
order by id desc
limit 1) as column4
from table1
join table2 on table1.id = table2.id
join table3 on table2.id = table2.id
where clause...
私が持っているsqlalchemyの同等物は
order_state = aliased(Table1)
po_state = aliased(Table1)
proof_state = aliased(Table1)
subq = meta.Session.query(Table2.exception_message).\
filter(Table2.entity_id.in_([Table3.id, Table4.id, Table5.id])).\
order_by(Table2.id.desc()).\
limit(1).label('exception')
query = meta.Session.query(
Table3.po_number, Table4.id.label('po_id'), Order.id.label('order_id'),\
Table5.id.label('proof_id'), order_state.description.label('order_state'),\
po_state.description.label('po_state'), proof_state.description.label('proof_state'),
Table3.order_placement_date, subq).\
outerjoin(Table4, Table3.id == Table4.order_id).\
outerjoin(Table5, Table4.id == Table5.po_id).\
outerjoin(order_state, Table3.state_diagram_state_id == order_state.id).\
outerjoin(po_state, Table4.state_diagram_state_id == po_state.id).\
outerjoin(proof_state, Table5.state_diagram_state_id == proof_state.id).\
filter(Table3.is_placed != 0).\
filter(not_(Table3.po_number.contains('%-%'))).\
filter(not_(Table3.state_diagram_state_id.in_(Table1.NONE_ORDER_EXCEPTIONS))).\
filter(or_(Table3.state_diagram_state_id.in_(Table1.ORDER_EXCEPTIONS),\
Table4.state_diagram_state_id.in_(Table1.PO_EXCEPTIONS),\
Table5.state_diagram_state_id.in_(Table1.PROOF_EXCEPTIONS)))
クエリしている列をそのフィルターのパラメーターとして使用するSQL錬金術でサブクエリをどのように使用しますか?
とても有難い。