Skip to content
Advertisement

Union ALL with different number of columns

I have an Employee table with these columns :

  • EmployeeId
  • Fullname
  • Phone
  • Department
  • Team
  • Function
  • Manager

and EmployeeHistory containing the history with different columns, but there are some in common :

  • EmployeeId
  • Fullname
  • Email
  • Geolocation
  • Department
  • Team
  • Function
  • Manager

How can I union them?

Advertisement

Answer

Replace uncommon columns by dummy values.

SELECT 
EmployeeId ,
Fullname   ,
'Undefined' AS Email,
Phone      ,
'Undefined' AS Geolocation
Department ,
Team       ,
[Function]   ,
Manager    
FROM Employee

UNION ALL

EmployeeId ,
Fullname   ,
Email      ,
'0000000000' AS Phone,
Geolocation,
Department ,
Team       ,
[Function]   ,
Manager    
FROM EmployeeHistory
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement