在Master-Detail上扭曲

时间:2011-06-02 14:16:24

标签: tsql join master-detail

至少对我来说,我有一个相当困难的要求。

我有一个包含多个详细信息表的主表。所有这些表只返回null或一个记录,但一个表将返回null或更多[除了一个]。

加入和左连接Multi-detail表之前的表(不知道还有什么叫它)是简单,直接的东西,但我如何检索多个记录?我终于找到了使用正确加入的理由吗?

如果没有发布太多的表脚本,这里是提取所有数据的SP:

SELECT 
Asclepius.GetFullPatientName(P.FirstNameAr,P.SecondNameAr, P.ThirdNameAr, P.FourthNameAr) AS PatientName,
PA.AdmittanceID,
PInvoice.InvoiceSerialNo,
PInvoice.InvoiceTotal,
PInvoice.InvoiceID,
--EmergencyWard
EW.Wages, EW.Bandages,EW.ECG, EW.Equipment,EW.FirstAid,EW.MedicalSupplies,EW.Medicine,EW.Other,
--Maternity
M.Wage,M.Graphing,M.Equipment,M.Medicine,M.MedicalSupplies,M.MaternityOperatingRoom,M.MaternityOpeartionFees,M.Other,
--RoomService
RS.Stay,RS.Cohabitant,RS.Medicine,RS.MedicalSupplies,RS.Oxygen,RS.MonitorUsage,RS.ECG,RS.Telephone,RS.Cafeteria,RS.Other,
--Nursery
N.Bed,N.Incubator,N.LightIncubator,N.Medicine,N.MedicalSupplies,N.Equipment,N.Other,
--Lab And Radiology
LR.LabTests,LR.BloodBank,LR.Radiology,LR.Ultrasound,LR.CTScan,LR.Endoscopy,LR.MRI,LR.Catheter,LR.Other,
--Surgery
S.ORFees,S.MedicalSupplies,S.Medicine,S.Equipment,S.SurgeryShifts,S.Other,
--Doctors
D.DoctorID,D.Wage,CD.DoctorNameAr

FROM
--PatientInvoice
Accounting.PatientInvoice AS PInvoice
INNER JOIN EMR.Patient AS P ON PInvoice.PatientID = P.PatientID
INNER JOIN EMR.PatientAdmittance AS PA ON P.PatientID = PA.PatientID AND     PInvoice.AdmittanceID = PA.AdmittanceID
--EmergencyWard
LEFT JOIN Accounting.PatientInvoice_EmergencyWard AS EW ON PInvoice.PatientID =     EW.PatientID AND PInvoice.AdmittanceID = EW.AdmittanceID AND PInvoice.InvoiceSerialNo = EW.InvoiceSerialNo
--Maternity
LEFT JOIN Accounting.PatientInvoice_Maternity AS M ON PInvoice.PatientID = M.PatientID AND PInvoice.AdmittanceID = M.AdmittanceID AND PInvoice.InvoiceSerialNo = M.InvoiceSerialNo
--RoomService
LEFT JOIN Accounting.PatientInvoice_RoomService AS RS ON PInvoice.PatientID = RS.PatientID AND PInvoice.AdmittanceID = RS.AdmittanceID AND PInvoice.InvoiceSerialNo = RS.InvoiceSerialNo
--Nursery
LEFT JOIN Accounting.PatientInvoice_Nursery AS N ON PInvoice.PatientID = N.PatientID AND PInvoice.AdmittanceID = N.AdmittanceID AND PInvoice.InvoiceSerialNo = N.InvoiceSerialNo
--Lab And Radiology
LEFT JOIN Accounting.PatientInvoice_LabAndRadiology AS LR ON PInvoice.PatientID = LR.PatientID AND PInvoice.AdmittanceID = LR.AdmittanceID AND PInvoice.InvoiceSerialNo = LR.InvoiceSerialNo
--Surgery
LEFT JOIN Accounting.PatientInvoice_Surgery AS S ON PInvoice.PatientID = S.PatientID AND PInvoice.AdmittanceID = S.AdmittanceID AND PInvoice.InvoiceSerialNo = S.InvoiceSerialNo
--Doctors
LEFT JOIN Accounting.PatientInvoice_Doctors AS D ON PInvoice.PatientID = D.PatientID AND PInvoice.AdmittanceID = D.AdmittanceID AND PInvoice.InvoiceSerialNo = D.InvoiceSerialNo
LEFT JOIN EMR.ConsultantDoctors AS CD ON D.DoctorID = CD.DoctorID

罪魁祸首是最后两人加入评论 - 博士。所有前面的表只返回0或1条记录,最后一个表可以提供0或n。

或者只是简单地使用MARS并独立检索那个?

TIA

1 个答案:

答案 0 :(得分:0)

你知道一个事实是它没有返回n行,因为这是连接应该做的事情吗?如果列类型匹配,则可以使用UNION。