问题描述
- oracl数据库sql语句怎么查询工资最低的两名员工编号,姓名,工资等信息。
-
查询每个部门工资最低的两个员工编号,姓名,工资,等信息。关键是工资最低的两名员工
解决方案
SELECT
*
FROM
(
SELECT
bumen,
price,
ROW_NUMBER () OVER (
PARTITION BY bumen
ORDER BY
bumen,
price DESC
) rn
FROM
table1
WHERE
bumen IN (
SELECT
bumen
FROM
table1
GROUP BY
bumen
)
)
WHERE
rn < 3
解决方案二:
我改了一下上面的代码,加了一个表的别称:
SELECT *
FROM ( SELECT a.* ,
ROW_NUMBER() OVER ( PARTITION BY 部门 ORDER BY 工资 ASC ) rn
FROM 表 a
) a
WHERE rn <= 2
解决方案三:
select top(2) from (select * from emp order by salary desc)
解决方案四:
select * from (
select a.*, row_number()over(partition by 部门 order by 工资) rn from 表
) where rn<=2
解决方案五:
select * from emp order by salary asc, 他们写的好像都是sqlserver的, 用这条语句查询最前面两天条就好了。
解决方案六:
select * from emp order by salary asc limit 2好像就可以
时间: 2024-11-01 21:21:53