tblPickList has the list of procedures. tblPickListItem lists each supply and quantity used in the procedure, while tblSupplies has the cost for each item.
The following code is supposed to list the procedure and average cost for the procedure. For the SubTotal column, I sum the cost of all items being used and divide by the number of procedures with an identical name. I have verified that replacing "SAMPLE NAME" with any given procedure will give me the correct number to get the average, but I can't seem to find a variable that I can use to change the number of procedures for each new record in the query.
Expand|Select|Wrap|Line Numbers
- SELECT tblPickList.[Procedure Name], (Sum([Number Open]*[Cost]+[Number PRN]*[Cost]))/(SELECT Count(Physician) as PhysicianCount
- FROM tblPickList GROUP BY [Procedure Name] HAVING [Procedure Name] = "SAMPLE NAME" ) AS SubTotal
- FROM tblSupplies INNER JOIN (tblPickList INNER JOIN tblPickListItem ON tblPickList.PickListID = tblPickListItem.PickListID) ON tblSupplies.[Supply name] = tblPickListItem.[Supply name]
- GROUP BY tblPickList.[Procedure Name];