Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Wie kombiniere ich meine Top-5-Gehälter und die Bottom-5-Gehälter, um sie in Oracle anzuzeigen?

Verwenden Sie UNION oder 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;

Sie können dies auch prägnanter schreiben als:

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;