如何将我的前5名工资和后5名工资结合起来在Oracle中显示?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将我的前5名工资和后5名工资结合起来在Oracle中显示?相关的知识,希望对你有一定的参考价值。
所以我知道如何让前5名和后5名自己展示。问题是如何将两者结合起来同时显示。这就是我所拥有的,但它只显示了底部5。
SELECT SAL FROM
(
SELECT DISTINCT SAL FROM EMP WHERE SAL IS NOT NULL ORDER BY SAL DESC
)
WHERE ROWNUM <6 AND
SELECT SAL FROM
(
SELECT DISTINCT SAL FROM EMP WHERE SAL IS NOT NULL ORDER BY SAL ASC
)
WHERE ROWNUM <6;
答案
使用UNION
或UNION ALL
:
SELECT SAL FROM
(
SELECT DISTINCT SAL FROM EMP WHERE SAL IS NOT NULL ORDER BY SAL DESC
)
WHERE ROWNUM < 6
UNION ALL
SELECT SAL FROM
(
SELECT DISTINCT SAL FROM EMP WHERE SAL IS NOT NULL ORDER BY SAL ASC
)
WHERE ROWNUM < 6;
你也可以更简洁地写这个:
SELECT SAL
FROM (SELECT DISTINCT SAL,
DENSE_RANK() OVER (ORDER BY sal) as seqnum_asc,
DENSE_RANK() OVER (ORDER BY sal) as seqnum_desc
FROM EMP
WHERE SAL IS NOT NULL
) s
WHERE seqnum_asc < 6 OR seqnum_desc < 6;
以上是关于如何将我的前5名工资和后5名工资结合起来在Oracle中显示?的主要内容,如果未能解决你的问题,请参考以下文章
在Oracle 里系统自带的2个表employees和department,用这2张表 如何写出高于IT部门平均工资的部门名