2014-09-26 173 views
0

請考慮下面的這個表格並給出結果集。現在,只要滿足其中一個條件,我如何才能獲得一條記錄。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

您可以用'rownum'功能 – Aramillo 2014-09-26 18:28:54

+0

可能的工作,但如果第一個條件不滿足什麼(IE)沒有記錄存在這個condn,第二個條件通行證和應只返回第二行等等。 – Remo 2014-09-26 18:32:26

+0

'ROWNUM'會從整個結果集中隨機返回有限的記錄。但是,OP要爲每個條件設置「DISTINCT」行。 – 2014-09-26 18:35:14

回答

0

爲了得到只有一個記錄滿足每一個條件,在SELECT聲明適用DISTICT

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;