Jump to content

Recommended Posts

I have tried for ages but can't get a LEFT JOIN to work on this. I want ALL table 1 records listed and done so any records in table 2 (that apply) are taken into account (and the deduction amount removed from wages).

 

If month is 32 now, then the query would return:

Dave Smith / 2000

Robert Brown / 2200

 

 

Table 1 "Employees"

 

employeeDept / deptEmployeeRef / name / wage

 

A / 2 / Dave Smith / 2000

B / 2 / Robert Brown / 2500

 

 

Table 2 "Deductions"

 

monthNo / employeeDept / deptEmployeeRef / deduction

 

32 / B / 2 / 200

32 / B / 2 / 100

32 / C / 2 / 300

32 / A / 3 / 300

33 / B / 2 / 500

try :

select a.employeeDept,
       a.deptEmployeeRef,
       a.name,
       a.wage -  SUM(IFNULL(b.deduction,0)) AS wages
FROM employees AS a 
   LEFT JOIN deductions AS b 
        ON a.employeeDept = b.employeeDept
        AND a.deptEmployeeRef = b.deptEmployeeRef
        AND b.monthNo = 32
GROUP BY a.employeeDept, a.deptEmployeeRef;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.