Skip to content
Advertisement

mysql: Select Max value and delete other rows

I need to get the row with the maximum status value for each employee_id and keep only one record for employee_id

If status is equal for the same employee id then pick one and delete the other, Thank you and appreciated

Table:

ID Type Status emplyee_ID
1111 A 0 10
2222 A 1 10
3333 B 0 20
4444 B 0 20

Desired out but

ID Type Status Emplyee ID
2222 A 1 10
3333 B 0 20

Advertisement

Answer

try with this …

SELECT emp.*
FROM employer_status emp,
    (SELECT emp1.emplyee_id, MAX(emp1.id) id
        FROM employer_status emp1, 
            (SELECT emp3.emplyee_id AS emplyee_id, MAX(emp3.status) AS STATUS
               FROM employer_status emp3
               GROUP BY emp3.emplyee_id
            ) AS emp2
        WHERE emp1.emplyee_id = emp2.emplyee_id
        AND emp1.status = emp2.status
        GROUP BY emp1.emplyee_id
    ) emp4
WHERE emp.id = emp4.id;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement