0

だから私はこのテーブルを持っています

Col1    Col2    Col3
A       34       X
B       43       L
A       36       L

今私がクエリすると

select * from Table1 where col1 in ('A','B','C')

私は次のようなものを期待しています

Col1    Col2    Col3
A       34       X
B       43       L
A       36       L
C       -        -

出来ますか ?

PS:-行 C は、列が空であることを示すためのものです。

4

3 に答える 3

1

ネストしたテーブル スキーマ オブジェクト タイプを作成できます。

create type T_List1 as table of varchar2(100);

次に、次のようにクエリを作成します。

 select s.column_value            as col1
      , nvl(to_char(t.col2), '-') as col2
      , nvl(col3, '-')            as col3
  from Table1 t
 right join table(T_List1('A', 'B', 'C')) s
    on (t.col1 = s.column_value)

例:

-- sample of data from your question
with Table1(Col1, Col2, Col3) as(
  select 'A',  34,  'X' from dual union all
  select 'B',  43,  'L' from dual union all
  select 'A',  36,  'L' from dual
)  -- actual query
 select s.column_value            as col1
      , nvl(to_char(t.col2), '-') as col2
      , nvl(col3, '-')            as col3
   from Table1 t
  right join table(T_List1('A', 'B', 'C')) s --< here list your values
     on (t.col1 = s.column_value)            -- as you would using `IN` clause

結果:

COL1  COL2   COL3
------------------------
A     36     L    
A     34     X    
B     43     L    
C     -      -   

SQLFiddle デモ

于 2013-09-23T19:28:56.333 に答える
0

Nicholas Krasnovの回答のように追加のネストされたテーブルタイプを作成したくない場合、またはA、B、C行で別の一時テーブルを作成したくない場合は、with句を使用して駆動テーブルを作成するだけです:

with driving_table(col) AS
(
  select 'A' from dual
  union 
  select 'B' from dual
  union
  select 'C' from dual

)
select dt.col            as col1
     , nvl(to_char(t.col2), '-') as col2
     , nvl(col3, '-')            as col3
  from Table1 t
 right join driving_table dt
  on (t.col1 = dt.col)

http://sqlfiddle.com/#!4/112ef/2

于 2013-09-23T20:26:37.793 に答える