具有Oracle子查询的情况不起作用

时间:2014-07-12 07:09:15

标签: sql oracle oracle10g

我在Oracle的CASE查询中遇到了一些问题

select
      case 
          when substr(object_subtype,0,1) = '8'
            then 
           'Planatias'
          when substr(object_subtype,0,1) = '1'
             then 
              'Licence'
          when substr(object_subtype,0,1) = '4'
             then 
              'PMA'        
          when substr(object_subtype,0,1) = '7'
             then 
              'Location'                
          else
             'no'
      end objectType,
  id ,substr(object_subtype,0,1)
  from amatia_logtask order by 1 

现在我的问题是我为每个数字提供了4个不同的表格

select * from amatia_licencias ;

select * from amatia_locacion ;

select * from amatia_pma;

select id_plantilla from amatia_plantillas;

我希望这4个表中的特定字段与他们在CASE语句中的ID

相关

但是像这样查询


select
      case 
          when substr(object_subtype,0,1) = '8'
            then 
              select id_pma from amatia_plantillas where id_plantilla =  substr(object_subtype,3) 
          when substr(object_subtype,0,1) = '1'
             then 
              'Licence'
          when substr(object_subtype,0,1) = '4'
             then 
              'PMA'        
          when substr(object_subtype,0,1) = '7'
             then 
              'Location'                
          else
             'no'
      end objectType,
  id ,substr(object_subtype,0,1)
  from amatia_logtask order by 1 

对我不起作用

给出此错误

ORA-00936: missing expression
00936. 00000 -  "missing expression"
*Cause:    
*Action:
Error at Line: 5 Column: 15

2 个答案:

答案 0 :(得分:1)

CASE语句return expressions:SELECT语句不计入此上下文中的表达式。所以不要在这里使用CASE,使用外连接。

您还没有为我们提供足够的详细信息以保证正常运行的SQL,因此您必须从中挑选出这些骨骼:

with logtask as ( select  id 
                          , substr(object_subtype,0,1) as st_1
                          , substr(object_subtype,3) as st_3
                  from amatia_logtask 
                  )
select logtask.id
       , logtask.st_1
       , coalesce ( apla.id_pma
                    , alic.id_blah
                    , aloc.id_meh
                    , apma.id_etc
                    , 'no' ) as whatever
from   logtask
       left join amatia_plantillas pla
            on logtask.st_1 = apla.id_plantilla
       left join amatia_licencias alic
            on logtask.st_1 = alic.id_licencia
       left join amatia_locacion aloc
            on logtask.st_1 = aloc.id_locacion
       left join amatia_pma apma
            on logtask.st_1 = apma.id_pma
order by 3, 1 
/

答案 1 :(得分:1)

可以使用子查询 - 但它们必须只有一行和一列。考虑列/行中查询的结果:

| COL1 | COL2 |
|------|------|
|    a |    x |
|    b |    y |

每个"单元"保留一个值(仅一个值)

| COL1 | COL2                                                                 |
|------|----------------------------------------------------------------------| 
|    a | cannot be a "select *" subquery because that is more than one column |
|    b |    y                                                                 |

因此, IF 在select子句中使用子查询,它们只能返回一个值(一行,一列),因此必须仔细编写子查询

    CREATE TABLE A_TABLE
        ("COL1" varchar2(1), "COL2" varchar2(1))
    ;

    INSERT ALL 
        INTO A_TABLE ("COL1", "COL2")
             VALUES ('a', 'x')
        INTO A_TABLE ("COL1", "COL2")
             VALUES ('b', 'y')
    SELECT * FROM dual
    ;

**Query 1**:

    select
      case
           when col1 = 'a' then (select 'subquery 1' from dual)
           when col1 = 'b' then (select 'subquery 2' from dual)
           else (select 'one value' from dual)
      end as col1_case
    from a_table


**[Results][2]**:

    |  COL1_CASE |
    |------------|
    | subquery 1 |
    | subquery 2 |

http://sqlfiddle.com/#!4/76041/3

我不推荐它,我只是展示了这种方法。如果可行,我会优先使用连接。