0

I have two tables with employee information and I want to select the record with latest date between those tables.

Table1  
EmpNum      Name      StartDate      refrance  
1234        John      05Dec2009      999999    
1255        Carl      06May2003      454545  
1236        Smith     15Jan2011      889898

Table2  
EmpNum      Name      StartDate      refrance  
1234        John      08Feb2014      111111    
1255        Carl      25Jul2001      454545  
1236        Smith     15Jan2011      889898  

Expected output  
EmpNum      Name      StartDate      refrance  
1234        John      08Feb2014      111111    
1255        Carl      06May2003      454545  
1236        Smith     15Jan2011      889898

StanislavL
  • 56,971
  • 9
  • 68
  • 98
Mr.M
  • 1

2 Answers2

1

Try like this

SELECT EmpNum,Name,Max(StartDate),refrance 
FROM (
      SELECT * FROM Table1
      Union All
      SELECT * FROM Table2
      ) AS T
Group By EmpNum,Name
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
1
select 
  EmpNum, 
  Name,
  if(t1.startDate>t2.startDate,t1.startDate,t2.startDate) as maxDate,
  if(t1.startDate>t2.startDate,t1.refrance,t2.refrance) as refrance
from table1 t1
     join table2 t2 on t1.EmpNum=t2.EmpNum
StanislavL
  • 56,971
  • 9
  • 68
  • 98