2013-04-16 33 views
0

我有三個表這樣的 - 醫院DiagnossticFarmacy求和從SQL視圖所得的值作爲總

醫院

HospitalID int 
PatientID int 
Due   float 

Diagnosstic

DaignosticID int 
PatientID int 
Due   float 

Farmacy

FarmacyID int 
PatientID int 
Due   float 

patientID柱是病人

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 
+0

什麼ü通過由於總的意思是...是指三個或inddividual categore(FRAM,HOSP的總和的總和,diag) –

回答

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 
+0

是的,它的作品.... thanxx很多.... – user2180894