Home > Back-end >  Query the Latest Data Based On Condition in Multiple Columns and Concat Them
Query the Latest Data Based On Condition in Multiple Columns and Concat Them

Time:01-28

I have a database like this

enter image description here

I think you missed "(" after "concat".

SELECT CONCAT(first_name,' ',last_name,' ',salary,' ',title) As Current Employees
FROM employees WHERE s_from_date = (SELECT MAX(from_date) FROM employees) AND t_from_date = (SELECT MAX(from_date) FROM employees)

CodePudding user response:

You could try

SELECT CONCAT(first_name,' ',last_name,' ',salary,' ',title) As "Current Employees" 
FROM employees e1
WHERE s_from_date = (SELECT MAX(s_from_date) FROM employees e2
                    WHERE e2.first_name = e1.first_name AND e2.last_name = e1.last_name) 
     AND t_from_date = (SELECT MAX(t_from_date) FROM employees e3
                    WHERE e3.first_name = e1.first_name AND e3.last_name = e1.last_name);

If your employees table has id/employee_id column, instead of using last_name and first_name in condition of 2 subqueries, you could change it to e2.id = e1.id, e3.id = e1.id.

  • Related