Combine 2 queries with sum involved

前端 未结 1 1931
攒了一身酷
攒了一身酷 2021-01-28 11:47

I have 2 similar queries that both look like this with only the table being switched from Treatment to Patient in the second query:

SELECT Treatment.Phys_ID AS P         


        
相关标签:
1条回答
  • 2021-01-28 12:25

    You can use subqueries to add the charges at the Physician level:

    SELECT Physician.Phys_ID AS Phys_ID, Physician.FName, Physician.LName, 
    
      (SELECT Nz(Sum(Treatment.Charge)) 
       FROM Treatment WHERE Treatment.Phys_ID = Physician.Phys_ID) +
    
      (SELECT Nz(Sum(Patient.Charge))
       FROM Patient WHERE Patient.Phys_ID = Physician.Phys_ID) As Total Charge
    
    FROM Physician;
    

    Alternatively, you can use DSum (strictly an MS Access function and not ANSI SQL).

    SELECT Physician.Phys_ID AS Phys_ID, Physician.FName, Physician.LName, 
    
       Nz(DSum("Charge", "Treatment", "Phys_ID =" &  Physician.Phys_ID)) +
    
       Nz(DSum("Charge", "Patient", "Phys_ID =" & Physician.Phys_ID)) As Total Charge
    
    FROM Physician;
    
    0 讨论(0)
提交回复
热议问题