Това:
select final.deptId, d.deptName,
e3.employeename + ',' + cast(e3.salary as varchar) employee
from employee e3
left join (
select e1.id, e1.deptId from employee e1
left join employee e2
on e1.deptId = e2.deptId and e1.id > e2.id
where e2.id is null
) final on e3.id = final.id
left join department d on d.id = final.deptId
Резултати в:
+--------+----------+-------------+ | DEPTID | DEPTNAME | EMPLOYEE | +--------+----------+-------------+ | 1 | IT | John,10000 | | | | Bob,15000 | | 2 | CSE | Akon,12000 | | | | Smith,20000 | +--------+----------+-------------+
Обърнете внимание, че "празните" стойности всъщност са попълнени с null
стойности.
Уведомете ме, ако имате проблем с него.