答案 0 :(得分:0)
我假设您要求返回患者上次访问特定医生的记录,并且您将由患者和医生进行过滤。如果是这样,试试这个:
SELECT *
FROM
(
SELECT *
ROW_NUMBER() OVER(ORDER BY date DESC PARTITION BY patient_id, doctor_id) rn
FROM PatientsTable
) a
WHERE rn = 1
AND patient_id = 1234
AND doctor_id = 5678
答案 1 :(得分:0)
我认为您希望看到患者第一次就诊或重复就诊。如果是,则可以使用此查询。
SELECT V.visit_id, V.patient_id, V.doctor_id, V.dd, V.h, V.mins,
( CASE WHEN VST.visit_id IS NOT NULL THEN 1 ELSE 0 END ) repeat_visit
FROM
visit_table V
OUTER APPLY( SELECT top 1 V1.visit_id FROM visit_table V1 WHERE V.visit_id > V1.visit_id
AND V.patient_id = V1.patient_id ) AS VST