Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Query of Queries

Status
Not open for further replies.

tsyle

Programmer
May 16, 2005
35
US
Ok I have to find the total charge for procedures of the patient with the higest total payment. Here are some data

Procedure_Code, Procedure_Description, Procedure_Charge
CXR,Chest X-Ray,55.00
REX,Routine Exam,40.00
AEX,Annual Exam,350.50
SLB,Setting Leg Bone,110.00
SAB,Setting Arm Bone,95.50

Visit_Number, Procedure_Code, Number_Of_Times_Procedure_Was_Applied_This_Visit
1,REX,1
1,EEX,1
1,BDR,1
2,REX,3
2,EEX,1
3,BDR,1
3,AEX,1
3,CXR,1
4,REX,2
4,BDR,1

Payment_Number, Patient_ID, Payment_Date, Amount_Of_Payment
1,7698,2003-01-20,75.00
2,3832,2003-01-20,24.50
3,9283,2003-01-20,98.23
4,8392,2003-01-20,10.10
5,5382,2003-01-20,12.14
6,1328,2003-01-21,25.32

Visit_Number, Patient_ID, Visit_Date, Appointment_Time
1,9283,2003-01-20,09:00:00AM
2,6372,2003-01-20,09:15:00AM
3,7347,2003-01-20,09:30:00AM
4,2372,2003-01-20,09:45:00AM
5,8493,2003-01-20,10:00:00AM
6,4283,2003-01-20,02:00:00PM

SELECT VP.Procedure_Code, OP.Procedure_Description,
(VP.Number_Of_Times_The_Procedure_Was_Applied_This_Visit
* OP.Procedure_Charge)AS Total_Procedure_Charge
FROM Payment V, Visit_Procedure VP, Visit VS, Off_Procedure OP
WHERE VP.Visit_Number = VS.Visit_Number
AND VS.Patient_ID = V.Patient_ID
AND OP.Procedure_Code = VP.Procedure_Code
GROUP BY V.Patient_ID, VP.Procedure_Code, OP.Procedure_Description,
OP.Procedure_Charge, VP.Number_Of_Times_The_Procedure_Was_Applied_This_Visit
HAVING SUM(Amount_Of_Payment) >= All
(SELECT SUM(Amount_Of_Payment)
FROM Payment V
GROUP BY Patient_ID)
ORDER BY VP.Procedure_Code;


This is what I got so far, and that returns the Patient with the highest total payments along with the procedures that he/she has used and the cost of each procedure. All I need to do is add up the Total_Procedure_Charge column but i am stuck. Can't figure it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top