4个查询以防案件陈述

时间:2019-01-30 09:26:44

标签: sql oracle case

我有4个查询

1.query = (select /*smthing*/ from my_table where /*conditions*/ and rownum = 1)
2.query = (select /*smthing*/ from my_table where /*conditions*/ and rownum = 1)
3.query = (select /*smthing*/ from my_table where /*conditions*/ and rownum = 1)
4.query = (select /*smthing*/ from my_table where /*conditions*/ and rownum = 1)

所有查询都工作正常。

我要使用CASE statement

 Case 
      when 1.query = NULL then 2.query
      when 2. query = NULL then 3.query
      else 4.query
 END

我只想得到1个结果。但是它返回的结果不止1个。

我该如何解决?

4 个答案:

答案 0 :(得分:0)

在SQL中,=NULL不一起使用。始终尝试使用IS NULLNOT NULL等。尝试如下操作:

Case 
      when 1.query IS NULL then 2.query
      when 2. query IS NULL then 3.query
      else 4.query
 END

答案 1 :(得分:0)

在Scott的架构上,其中包含以下数据:

SQL> select * from dept order by deptno;

    DEPTNO DNAME                LOC
---------- -------------------- --------------------
        10 ACCOUNTING           NEW YORK
        20 RESEARCH             DALLAS
        30 SALES                CHICAGO
        40 OPERATIONS           BOSTON

SQL> select * from emp  order by deptno;

     EMPNO ENAME      JOB              MGR HIREDATE        SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
      7782 CLARK      MANAGER         7839 09.06.81       2450                    10
      7839 KING       PRESIDENT            17.11.81      10000                    10
      7934 MILLER     CLERK           7782 23.01.82       1300                    10
      7566 JONES      MANAGER         7839 02.04.81       2975                    20
      7902 FORD       ANALYST         7566 03.12.81       3000                    20
      7876 ADAMS      CLERK           7788 12.01.83       1100                    20
      7369 SMITH      CLERK           7902 17.12.80        920                    20
      7788 SCOTT      ANALYST         7566 09.12.82       3000                    20
      7521 WARD       SALESMAN        7698 22.02.81       1250        500         30
      7844 TURNER     SALESMAN        7698 08.09.81       1500          0         30
      7499 ALLEN      SALESMAN        7698 20.02.81       1600        300         30
      7900 JAMES      CLERK           7698 03.12.81        950                    30
      7698 BLAKE      MANAGER         7839 01.05.81       2850                    30
      7654 MARTIN     SALESMAN        7698 28.09.81       1250       1400         30

14 rows selected.

SQL> select * from bonus;

ENAME      JOB              SAL       COMM
---------- --------- ---------- ----------
KING       PRESIDENT       1000        100

使用UNION的查询(这意味着您使用的所有查询的列列表必须在数量和数据类型上匹配)看起来像这样;返回的第一列显示结果集所属的查询。

SQL> with
  2    q1 as (select 'q1' what, deptno, dname, null from dept  where deptno = &par_deptno),
  3    q2 as (select 'q2' what, deptno, ename, job  from emp   where job = '&par_job'),
  4    q3 as (select 'q3' what, null  , ename, job  from bonus where ename = '&par_ename'),
  5    --
  6    qc1 as (select count(*) cnt from q1),
  7    qc2 as (select count(*) cnt from q2),
  8    qc3 as (select count(*) cnt from q3)
  9    --
 10  select q1.* from q1 join qc1 on 1 = 1 join qc2 on 1 = 1 where qc1.cnt > 0
 11  union all
 12  select q2.* from q2 join qc2 on 1 = 1 join qc1 on 1 = 1 where qc2.cnt > 0 and qc1.cnt = 0
 13  union all
 14  select q3.* from q3 join qc3 on 1 = 1 join qc1 on 1 = 1 join qc2 on 1 = 1
 15      where qc3.cnt > 0 and qc1.cnt = 0 and qc2.cnt = 0;
Enter value for par_deptno: 10
Enter value for par_job: CLERK
Enter value for par_ename: WHO

WH     DEPTNO DNAME                NULL
-- ---------- -------------------- ---------
q1         10 ACCOUNTING

SQL> /
Enter value for par_deptno: 15
Enter value for par_job: CLERK
Enter value for par_ename: WHO

WH     DEPTNO DNAME                NULL
-- ---------- -------------------- ---------
q2         20 SMITH                CLERK
q2         20 ADAMS                CLERK
q2         30 JAMES                CLERK
q2         10 MILLER               CLERK

SQL> /
Enter value for par_deptno: 15
Enter value for par_job: NONE
Enter value for par_ename: KING

WH     DEPTNO DNAME                NULL
-- ---------- -------------------- ---------
q3            KING                 PRESIDENT

SQL>

答案 2 :(得分:0)

您可以通过几种不同的方式来执行此操作,其中一种简单的方法是:

  select * from(
    (query1)
    UNION
    (query2)
     UNION
    (query3)
     UNION
    (query4)   ) where rownum = 1

它的意思是:

  select * from(
    (select /*smthing*/ from my_table where /*conditions*/ and rownum = 1)
    UNION
    (select /*smthing*/ from my_table where /*conditions*/ and rownum = 1)
     UNION
    (select /*smthing*/ from my_table where /*conditions*/ and rownum = 1)
     UNION
    (select /*smthing*/ from my_table where /*conditions*/ and rownum = 1)
  ) where rownum = 1

查询顺序很重要。

答案 3 :(得分:0)

我将所有这些组合成一个表达式:

select s.*
from (select /*smthing*/,
             row_number() over (order by case when <conditions1> then 1
                                              when <conditions2> then 2
                                              when <conditions3> then 3
                                              when <conditions4> then 4
                                              else 5
                                         end
                               ) as seqnum
      from my_table 
     ) s
where seqnum = 1