SQL从表中获取至少2个工资部门
SQL如何从表部门获取至少2份薪水 样本表:SQL从表中获取至少2个工资部门,sql,Sql,SQL如何从表部门获取至少2份薪水 样本表: empid salary Dept --------------------- 101 2000 aaa 102 1000 bbb 103 5000 bbb 104 8000 ccc 105 3000 aaa 106 4000 aaa 107 6000 ccc 108 7000 bbb 109 9000 ccc 输出应
empid salary Dept
---------------------
101 2000 aaa
102 1000 bbb
103 5000 bbb
104 8000 ccc
105 3000 aaa
106 4000 aaa
107 6000 ccc
108 7000 bbb
109 9000 ccc
输出应如下所示:
Dept empid salary
----------------------
aaa 101 2000
aaa 105 3000
bbb 102 1000
bbb 103 5000
ccc 104 6000
ccc 107 8000
如前所述,ccc部门中可能会有其他薪资为8000的人员将被遗漏。接下来,假设表名为samptab:
select dept, empid, salary
from samptab
where (
select count(*) from samptab as s
where s.dept = samptab.dept and s.salary <= samptab.salary
) <= 2 order by dept;
如果需要更多或更少的行,您可以更改查询行中的数字2。如果有关系怎么办?请澄清你的问题。这是否回答了你的问题?
select dept, empid, salary
from samptab
where (
select count(*) from samptab as s
where s.dept = samptab.dept and s.salary <= samptab.salary
) <= 2 order by dept;