Something like this should do the trick....
SELECT
emp.EmployeeName, sum_loan, sum_discount, sum_overTimeHours, count_attending,
(count_attending*SalaryPerDay) as totalDayPay,
(sum_overTimeHours*overTimeHoursPrice) as totalOverTimePay,
((count_attending*SalaryPerDay) + (sum_overTimeHours*overTimeHoursPrice) -
sum_discount - sum_loan) as grandTotal
FROM Employee emp
INNER JOIN (SELECT
EmployeeID,
sum(loan) as sum_loan,
sum(discount) as sum_discount,
sum(overTimeHours) as sum_overTimeHours,
sum(iif(Attending,1,0)) as count_attending
FROM Schedule
WHERE Date >= {date1} and Date <= {date2}
GROUP BY EmployeeID
) sch
ON emp.ID = sch.EmployeeID
WHERE emp.EmployeeName = {name}
Note the two WHERE
clauses. You can adjust these as needed to achieve your two different parameterized restrictions.
Edit #1:
Due to some uncertainty about the actual numeric value of the "boolean" stored in the Schedule.Attending
field, I've adjusted the query above to account for the boolean value explicitly. To accomplish this, I've made use of the MSAccess-specific expression function, IIF()
. This is a much more robust solution than just assuming that the field will contain either a 1
or a 0
.
Edit #2:
I should also note that the syntax varies slightly depending on where you're using it. The above is the "standard sql" syntax for the derived table (that's the subquery that's inside parenthesis following the INNER JOIN
keywords). If you're running this query through an ODBC connection, then the syntax above is valid.
However, If you're trying to create a Query within Access itself, you'll need to use square brackets with a trailing period [ ].
instead of parenthesis ( )
around the subquery. So instead of:
SELECT ... FROM Employee emp INNER JOIN (SELECT ... ) sch ON ...
use this:
SELECT ... FROM Employee emp INNER JOIN [SELECT ... ]. sch ON ...