deutz
Programmer
- May 23, 2013
- 3
Hi and thanks in advance,
(Using Access 2003)
I'm trying to create a query that brings together job/payment data from 4 related tables and group by client.
Table 1 tblClient has primary key as fldClientID and also has fldClientName
Table 2 tblJob has primary key as fldJobID and foreign key is fldClientID
Table 3 tblJobItem has dollar amounts for all jobs in fldItemAmt and foreign key is fldJobID
Table 4 tblPayment has dollar amounts paid for all jobs in fldPaymentAmt and foreign key is fldJobID
So I want to display one row for each client with the total amount incurred for all jobs and the total paid for all jobs.
I tried doing a left join between tblClient and tblJob and using two subqueries to get summed values from tblJobItem and tblPayment.
When attempting to run the SQL below I get error msg ... “You tried to execute a query that does not include the specified expression 'fldJobID' as part of the aggregate function.”
Any idea what I am doing wrong here?
SELECT tblClient.fldClientID, tblClient.fldClientName, (SELECT Sum(tblJobItem.fldItemAmt) FROM tblJobItem WHERE tblJobItem.fldJobID = tblJob.fldJobID) AS Total, (SELECT Sum(tblPayment.fldPaymentAmt) FROM tblPayment WHERE tblPayment.fldJobID = tblJob.fldJobID) AS Paid
FROM tblClient LEFT JOIN tblJob ON tblClient.fldClientID=tblJob.fldClientID
GROUP BY tblClient.fldClientID, tblClient.fldClientName
(Using Access 2003)
I'm trying to create a query that brings together job/payment data from 4 related tables and group by client.
Table 1 tblClient has primary key as fldClientID and also has fldClientName
Table 2 tblJob has primary key as fldJobID and foreign key is fldClientID
Table 3 tblJobItem has dollar amounts for all jobs in fldItemAmt and foreign key is fldJobID
Table 4 tblPayment has dollar amounts paid for all jobs in fldPaymentAmt and foreign key is fldJobID
So I want to display one row for each client with the total amount incurred for all jobs and the total paid for all jobs.
I tried doing a left join between tblClient and tblJob and using two subqueries to get summed values from tblJobItem and tblPayment.
When attempting to run the SQL below I get error msg ... “You tried to execute a query that does not include the specified expression 'fldJobID' as part of the aggregate function.”
Any idea what I am doing wrong here?
SELECT tblClient.fldClientID, tblClient.fldClientName, (SELECT Sum(tblJobItem.fldItemAmt) FROM tblJobItem WHERE tblJobItem.fldJobID = tblJob.fldJobID) AS Total, (SELECT Sum(tblPayment.fldPaymentAmt) FROM tblPayment WHERE tblPayment.fldJobID = tblJob.fldJobID) AS Paid
FROM tblClient LEFT JOIN tblJob ON tblClient.fldClientID=tblJob.fldClientID
GROUP BY tblClient.fldClientID, tblClient.fldClientName