すべてが順調であることを願っています。まず、私の SQL が良くなくて申し訳ありません。
次のような生のSQLがあります
select a.appointmentId, a.patientId, r.MaxTime from (
select appointmentid, patientid, max(apptDate) as MaxTime
from appointment
where facilityid=95
group by patientid
) r
inner join
Appointment a on
a.patientid = r.patientid and
a.apptDate = r.MaxTime
コードで SQLAlchemy の宣言型スタイルを使用しています。これがクエリの外観です
appt_query = alchemy_session.query(Appointment.appointmentId, Appointment.patientId, func.max(Appointment.apptDate).label('maxTime')).filter(
Appointment.facilityId == 95,
).group_by(Appointment.patientId).subquery()
appointments = alchemy_session.query(Appointment.appointmentId, Appointment.patientId, appt_query.c.maxTime).outerjoin(
appt_query, and_(
Appointment.patientId == appt_query.c.patientId,
Appointment.apptDate == appt_query.c.maxTime
)
)
しかし、私がするとき
予定を印刷する
残念ながら、私が望むSQLを生成していません。SQL の理解に誤りがあることはわかっているので、これに関する指針は本当に役に立ちます。お時間をいただき、ありがとうございました。