我编写了一个查询,它连接单独的表并尝试在某个时间点(插入点)发现哪些 PK 键是给定记录的数据库中最新的。您会看到我已将其缩减为患者 ID 100,因为这是我似乎可以使其正常工作的唯一方法。
当前查询的工作方式如下所示:
SELECT TOP 1 P1.PatientID,
P1.DimPatientPK,
DA1.DimAdmissionPK,
DD1.DiagnosisPK,
DI1.Investigation1PK,
DIE1.InvestigationECGPK,
IEG1.InvestigationEchoGoldPK,
MH1.DimMedicalHistoryPK,
FH1.DimPatientFamilyHistoryPK,
PHT1.PatientHospitalisationTreatmentPK,
PMP1.PatientMedicalPersonnelPK,
RR1.PatientReferralReasonPK,
PEA1.PhysicalExamAHSPK,
PEM1.PhysicalExamMurmursPK,
SI1.SocialIssuePK,
TRT.TreatmentPK
--DT1.Treatment1PK
FROM
DimPatient P1 LEFT JOIN DimAdmission DA1 ON P1.PatientID = DA1.PatientID
LEFT JOIN DimDiagnosis DD1 ON P1.PatientID = DD1.PatientID
LEFT JOIN DimInvestigation1 DI1 ON P1.PatientID = DI1.PatientID
LEFT JOIN DimInvestigationECG DIE1 ON P1.PatientID = DIE1.PatientId
LEFT JOIN DimInvestigationECHOgold IEG1 ON P1.PatientID = DIE1.PatientId
LEFT JOIN DimMedicalHistory MH1 ON P1.PatientID = MH1.PatientId
LEFT JOIN DimPatientFamilyHistory FH1 ON P1.PatientId = FH1.PatientID
LEFT JOIN DimPatientHospitalisationTreatment PHT1 ON P1.PatientID = PHT1.PatientId
LEFT JOIN DimPatientMedicalPersonnel PMP1 ON P1.PatientID = PMP1.PatientId
LEFT JOIN DimPatientReferralReason RR1 ON P1.PatientID = RR1.PatientId
LEFT JOIN DimPhysicalExamAHS PEA1 ON P1.PatientId = PEA1.PatientId
LEFT JOIN DimPhysicalExamination PE1 ON P1.PatientID = PE1.PatientId
LEFT JOIN DimPhysicalExamMurmurs PEM1 ON P1.PatientID = PEM1.PatientId
LEFT JOIN DimSocialIssue SI1 ON P1.PatientID = SI1.PatientID
LEFT JOIN DimTreatment TRT ON P1.PatientID = TRT.PatientId
WHERE P1.patientid IN(100)
ORDER BY DA1.DimAdmissionPK DESC,
P1.DimPatientPK DESC,
DD1.DiagnosisPK DESC,
DI1.Investigation1PK DESC,
DIE1.InvestigationECGPK DESC,
IEG1.InvestigationEchoGoldPK DESC,
MH1.DimMedicalHistoryPK DESC,
FH1.DimPatientFamilyHistoryPK DESC,
PHT1.PatientHospitalisationTreatmentPK DESC,
PMP1.PatientMedicalPersonnelPK DESC,
RR1.PatientReferralReasonPK DESC,
PEA1.PhysicalExamAHSPK DESC,
PE1.PhysicalExaminationPK DESC,
PEM1.PhysicalExamMurmursPK DESC,
SI1.SocialIssuePK DESC,
TRT.TreatmentPK DESC;
这成功地恢复了完整的记录,无论它是否已为 patid 100 填写。
我无法扩展它,以便它循环并为数据库中的每个患者收集相同的结果。
即,如果我删除 where 子句,我仍然只能得到 1 行..
如果我删除 select top 1 .. 然后它返回我多组患者 ID 90 - 我基本上想要每个患者 ID 有 1 行 - 即 90、91、92,每个表匹配的最大键值。
有人对如何实现这一目标有任何想法吗?