mysql - SQL: how to group by max date -
i'm pretty new sql, please excuse seems should basic query. i've been searching , trying convert other similar answer's code make mine work, can't seem right.
i have 2 tables. tables linked via unique employee id(emplid). table 1(job) houses various job related information title , table 2(title_changes) houses promotion actions, reasons, , dates. need develop query returns * both job , title_changes tables based on recent title_changes date.
fyi not mirror actual table data. oversimplifying make easier explain.
it should return: emlpid | title |titledate |titlechnagereason|requested_date 12345 | specialist 2| 02/01/2015| merit |01/31/2015 job table emplid | title | title date ---------+---------------+----------------+ 12345 | specialist 2 | 02/01/2015 --------+----------------+---------------- 12345 | specialist 1 | 09/01/2014 --------+----------------+---------------- 44576 | analyst 3 | 05/01/2015 --------+----------------+---------------- 44576 | analyst 2 | 03/01/2014 --------+---------------------+----------- title_changes table emplid | title change reason | requested_date ------------+---------------------+----------------+ 12345 | merit | 01/31/2015 ------------+---------------------+---------------- 12345 | newhire | 08/30/2014 ------------+---------------------+---------------- 44576 | promotion | 04/30/2015 ------------+---------------------+---------------- 44576 | newhire | 02/28/2014 ------------+---------------------+---------------- thanks help!
craig
try query
select j.emplid, substring_index(group_concat(j.title order j.titledate desc),',',1) title, substring_index(group_concat(j.titledate order j.titledate desc),',',1) title_date, substring_index(group_concat(tc.titlechnagereason order tc.requested_date desc),',',1) title_change_reson, substring_index(group_concat(tc.requested_date order tc.requested_date desc),',',1) request_date, job j join title_changes tc on(tc.emplid = j.emplid) group j.emplid
Comments
Post a Comment