0

我想结合 2 个查询来制作一个这样的结果

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 arow_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 回答