在这里,我有2个表EmployeeDepartment,数据如下。

Employee

Empid   Empname Deptid  salary
-----------------------------------------
1       rama    2       20000.00
2       sita    2       30000.00
3       gita    4       45000.00
4       rohit   4       40000.00
5       lata    5       50000.00
6       sami    2       23000.00
7       lala    3       35000.00
8       samta   4       41000.00
9       shika   5       55000.00
10      venu    4       4400.00


Department

Deptid DeptName       DeptReference
---------------------------------------
1      HR                1
2      Engineering       2
3      marketing         1
4      Planning          2
5      Admin             1
6      sales             2


所需的输出是


所有n个雇员的所有部门列表,其中包含雇员详细信息,其中n =部门的部门参考。
如果n超过该部门中的实际员工人数,则将显示尽可能多的现有员工,而其余员工将显示空值


输出:(Deptid, Deptname, empid, empname

提前致谢

纳伦德拉

最佳答案

您没有说要使用什么标准来选择要为每个部门查看的员工。我的示例显示了薪水最高的员工。您应该能够根据自己的选择轻松更改此设置。

select
    *
from
(
    select
        e.*
        ,d.DeptReference
        ,ROW_NUMBER() OVER (partition by e.Deptid order by e.salary desc) as Row
    from Employee as e
    inner join Department as d
        on e.Deptid = d.Deptid
)as xx
where xx.Row <= xx.DeptReference;

关于sql - 用引用栏列出员工,部门的详细信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23564616/

10-15 05:02