The query brings back the correct information in each column but i want to limit the output to only include the employee records with the lowest number in the ‘Date Difference’ column for each.
SELECT
associate_id, #employee unique identifier
reports_to AS 'Manager',
first_name AS 'First name',
last_name AS 'Last name',
job_code_title AS 'Job Title',
region AS 'Program',
timestamp AS 'When termination date was updated',
termination_date AS 'Termination date populated',
(DATEDIFF(timestamp, termination_date)) AS 'Date Difference'
FROM
worker_change
WHERE
status = 'Terminated'
AND termination_date LIKE '%2024%'
AND DATE(timestamp) <> termination_date
ORDER BY reports_to;
For example:
enter image description here
There are multiple timestamps for each associate_id so the ‘Date Difference’ column comes back with multiple results as shown in the screenshot. How can I make it so the ‘Date Difference’ only shows the record with the lowest number for each. Like, for this associate_id (employee record) I would only want it to come back with the timestamp showing the lowest number of the three results in the ‘Date Difference’.
I’ve tried MIN() functions but haven’t really made any progress.
Michael Llorente is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.