我需要从员工表中获取前2名工资 IAM使用下面的查询,但得到错误 请任何人能帮忙吗?
SELECT iICompanyID,
iIsequence
FROM employee
WHERE dIAmount IN (SELECT MAX(dIAmount)FROM employee)
OR dIAmount IN ((SELECT MAX(diamount)
FROM employee
WHERE diamount IN (SELECT dIAmount
FROM employee
WHERE dIAmount NOT IN (SELECT MAX(diamount)FROM employee)
)
答案 0 :(得分:1)
您可以使用DENSE_RANK
窗口功能来找到按薪水排序的员工。以下应按薪水返回前2名(如果有关系,则可能更多)的员工:
udf
答案 1 :(得分:0)
您可以将DISTINCT TOP(2)
与ORDER BY diamount DESC
一起使用,如下所示。
SELECT *
FROM employee
WHERE dIAmount IN (
SELECT DISTINCT TOP (2) dIAmount
FROM employee
ORDER BY dIAmount DESC
)
或者您可以使用JOIN
代替WHERE IN
条件,如下所示。
SELECT *
FROM employee E
INNER JOIN (
SELECT DISTINCT TOP (2) dIAmount AS dIAmount
FROM employee
ORDER BY dIAmount DESC
) M ON E.dIAmount = M.dIAmount