SELECT DATE_FORMAT(MAX(a.EffectiveDate),'%d-%m-%Y') AS `Date`,
CONCAT(a.FirstNm,' ',IFNULL(a.MiidleNM,' '),' ',IFNULL(a.LastNM,' ')) AS `EmployeeName`,
`b`.`BranchName` AS `Branch`
FROM `tbl_employeemaster` AS `a`
JOIN `tbl_branch` AS `b` ON a.BrnNm = b.idBranch
UNION
SELECT DATE_FORMAT(MAX(c.EffectiveDate),'%d-%m-%Y') AS `Date`,
CONCAT(c.FirstNm,' ',IFNULL(c.MiidleNM,' '),' ',IFNULL(c.LastNM,' ')) AS `EmployeeName `,
`d`.`BranchName` AS `Branch`
FROM `tbl_employeehistory` AS `c`
JOIN `tbl_branch` AS `d` ON c.BrnNm = d.idBranch
...is the output:
Date EmployeeName Branch
-------------------------------------
15-01-2011 ABCD Bangalore
10-10-2010 ABCD Magalore
...but i want the output as shown below
EmployeeName Branch Date 开发者_StackOverflow社区previous Branch Date
ABCD Bangalore 15-01-2011 Mangalore 10-10-2010
Try this:
SELECT a.EmployeeName,
a.Branch,
a.Date, b.branch previousBranch,
b.Date previousDate
FROM
( SELECT DATE_FORMAT(MAX(a.EffectiveDate),'%d-%m-%Y') AS `Date`,
CONCAT(a.FirstNm,' ',IFNULL(a.MiidleNM,' '),' ',IFNULL(a.LastNM,' ')) AS `EmployeeName`,
`b`.`BranchName` AS `Branch`
FROM `tbl_employeemaster` AS `a`
INNER JOIN `tbl_branch` AS `b` ON a.BrnNm = b.idBranch) a
LEFT JOIN
(SELECT DATE_FORMAT(MAX(c.EffectiveDate),'%d-%m-%Y') AS `Date`,
CONCAT(c.FirstNm,' ',IFNULL(c.MiidleNM,' '),' ',IFNULL(c.LastNM,' ')) AS `EmployeeName `,
`d`.`BranchName` AS `Branch`
FROM `tbl_employeehistory` AS `c`
INNER JOIN `tbl_branch` AS `d` ON c.BrnNm = d.idBranch) b
ON a.EmployeeName = b.EmployeeName
精彩评论