Oracle案例陈述

时间:2014-09-26 18:10:40

标签: oracle

考虑下表,并给出结果集。现在,只要满足其中一个条件,我如何只获得一条记录。

SQL> SELECT ename
  2  ,      job
  3  ,      CASE
  4            WHEN sal < 1000
  5            THEN 'Low paid'
  6            WHEN sal BETWEEN 1001 AND 2000
  7            THEN 'Reasonably well paid'
  8            WHEN sal BETWEEN 2001 AND 3001
  9            THEN 'Well paid'
 10            ELSE 'Overpaid'
 11         END AS pay_status
 12  FROM   emp;

ENAME      JOB       PAY_STATUS
---------- --------- --------------------
SMITH      CLERK     Low paid
ALLEN      SALESMAN  Reasonably well paid
WARD       SALESMAN  Reasonably well paid
JONES      MANAGER   Well paid
MARTIN     SALESMAN  Reasonably well paid
BLAKE      MANAGER   Well paid
CLARK      MANAGER   Well paid
SCOTT      ANALYST   Well paid
KING       PRESIDENT Overpaid
TURNER     SALESMAN  Reasonably well paid
ADAMS      CLERK     Reasonably well paid
JAMES      CLERK     Low paid
FORD       ANALYST   Well paid
MILLER     CLERK     Reasonably well paid

1 个答案:

答案 0 :(得分:0)

要仅为每个条件获得一条记录,请在DISTICT语句中应用SELECT

SQL> SELECT DISTINCT ename 2 , job 3 , CASE 4 WHEN sal < 1000 5 THEN 'Low paid' 6 WHEN sal BETWEEN 1001 AND 2000 7 THEN 'Reasonably well paid' 8 WHEN sal BETWEEN 2001 AND 3001 9 THEN 'Well paid' 10 ELSE 'Overpaid' 11 END AS pay_status 12 FROM emp;