Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi,I'm trying to figure out how to sum amounts generated by an attribute in one table, when the attribute and amount generated require some Joins....Each referral can refer more than one patient. Each patient can have multiple visits. Each visit generates one bill (one Total_Charges)
Select Referral.First_Name, Referral.Last_Name, Billing.Total_Charges as [Revenue]from Referralinner join Patient on Patient.Referral_ID = Referral.Referral_IDinner join PatientVisit on PatientVisit.Patient_ID = Patient.Patient_IDinner join Billing on Billing.Visit_ID = PatientVisit.Visit_IDgroup by sum(total_charges);
Right now this works fine, but every Referral who has sent multiple patients appears on a separate lines. So a referral by the name of John Smith comes back like this:John Smith 500.00John Smith 300.00John Smith 100.00but I need it to come back likeJohn Smith 900.00...possible? Thank you for your help!
TG
Master Smack Fu Yak Hacker
6065 Posts
Posted - 2007-12-03 : 12:37:38
Does this work?
Select Referral.First_Name, Referral.Last_Name, sum(Billing.Total_Charges) as [Revenue]from Referralinner join Patient on Patient.Referral_ID = Referral.Referral_IDinner join PatientVisit on PatientVisit.Patient_ID = Patient.Patient_IDinner join Billing on Billing.Visit_ID = PatientVisit.Visit_IDgroup by Referral.First_Name, Referral.Last_Name;
Be One with the OptimizerTG
invisible777
Starting Member
10 Posts
Posted - 2007-12-03 : 12:42:26
Works perfect!Thanks for the help (the very quick help!!) !