0

2つのクエリを組み合わせて、このような1つの結果を作成したい

Sandi | schemaid | value | Sandi | schemaid | value
100   | 2883     | 12324 | 200   | 2886     | 3456
120   | 2882     | 435   | 220   | 2887     | 555 
130   | 2881     | 3456  | 230   | 2888     | 333 

クエリは次のとおりです。

select y.Sandi , y.schemaid,y.value from tbl_schema y
where y.idx=1

select y.Sandi , y.schemaid,y.value from tbl_schema y
where y.idx=2

手伝って頂けますか?

4

2 に答える 2

1

個別のidxデータを行ではなく列に表示する必要があるため、次のように include a を使用row_number()して、row_number で個別のクエリを結合できます。

select
  q1.Sandi q1_Sandi,
  q1.schemaid q1_schemaid,
  q1.value q1_value,
  q2.sandi q2_Sandi,
  q2.schemaid q2_schema_id,
  q2.value q2_value
from
(
  select sandi, schemaid, value,
    row_number() over (order by sandi) rn
  from tbl_schema
  where idx = 1
) q1
full outer join
(
  select sandi, schemaid, value,
    row_number() over (order by sandi) rn
  from tbl_schema
  where idx = 2
) q2
  on q1.rn = q2.rn

デモで SQL Fiddle を参照してください

于 2013-03-11T10:44:30.927 に答える
0

両方のクエリで同じテーブル (tbl_schema) を使用しているため、次のようfor given ids (idx)に使用できると思いますfull outer join

select y.Sandi, y.schemaid, y.value, x.Sandi_, x.schemaid_, x.value_  
from tbl_schema y full outer join tbl_schema x
       on y.idx + 1 = x.idx
where y.idx = 1 and x.idx = 2
于 2013-03-11T10:57:25.133 に答える