0

我有三张这样的表 - HospitalDiagnossticFarmacy

医院

HospitalID   int
PatientID    int
Due         float

诊断

DaignosticID   int
PatientID    int
Due         float

农场

FarmacyID   int
PatientID    int
Due         float

患者ID列是来自患者表的pk

PatientId  int
Patient name  var

我有查询以显示来自不同表的患者的所有会费并将它们总计。

我成功检索了患者的所有会费,但我如何将它们相加?

我的查询是:

declare @patient int set @patient=1
BEGIN 
  SELECT 
   dbo.Master.Name, 
    dbo.Diagonistic.Due AS DaigiDue, 
    dbo.Farmacy.Due AS FramaDue,            
    dbo.Hospital.Due AS HospiDue 
 FROM dbo.Diagonistic 
 FULL OUTER JOIN dbo.Master ON dbo.Diagonistic.PatientId = dbo.Master.PatientId 
 FULL OUTER JOIN dbo.Farmacy ON dbo.Master.PatientId = dbo.Farmacy.PatientId 
 FULL OUTER JOIN dbo.Hospital ON dbo.Master.PatientId = dbo.Hospital.PatientId 
 where 
   dbo.Master.PatientId= @Patient 
end
4

1 回答 1

0

试试这个...

Declare @Patient Int 
Set @Patient=1
Begin 

    Select 
    dbo.Master.Name, 
    dbo.Diagonistic.Due AS DaigiDue, 
    dbo.Farmacy.Due AS FramaDue,            
    dbo.Hospital.Due AS HospiDue,
    Isnull(dbo.Diagonistic.Due,'0') +  Isnull(dbo.Farmacy.Due,'0') + Isnull(dbo.Hospital.Due,'0') as TotalDue     
    From dbo.Diagonistic 
    Full Outer Join dbo.Master ON dbo.Diagonistic.PatientId = dbo.Master.PatientId 
    Full Outer Join dbo.Farmacy ON dbo.Master.PatientId = dbo.Farmacy.PatientId 
    Full Outer Join dbo.Hospital ON dbo.Master.PatientId = dbo.Hospital.PatientId 
    Where 
        dbo.Master.PatientId= @Patient 

End
于 2013-04-16T06:47:28.597 回答