1
這裏是我的SQL查詢:如何在sqlalchemy ORM中表示此查詢?
select survey_spec.survey_spec_id, cr.login as created_by, installed_at, req.login as requested_by, role
from survey_spec
join (select survey_spec_id, role, max(installed_at) as installed_at from survey_installation_history group by 1, 2) latest using (survey_spec_id)
left join survey_installation_history using (survey_spec_id, role, installed_at)
left join users cr on created_by = cr.user_id
left join users req on requested_by = req.user_id
where survey_id = :survey_id
order by created_at desc, installed_at desc
我ORM實體survey_spec
,survey_installation_history
和users
,並survey_spec.installations
是使用survey_spec_id
爲重點,以survey_installation_history
的關係。