将sql视图中的结果值总计为总计

时间:2013-04-16 06:26:05

标签: sql subquery

我有三个这样的表 - 医院 Diagnosstic Farmacy

医院

HospitalID   int
PatientID    int
Due         float

Diagnosstic

DaignosticID   int
PatientID    int
Due         float

Farmacy

FarmacyID   int
PatientID    int
Due         float
来自患者表的 patientID 列是 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

1 个答案:

答案 0 :(得分: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