需要按特定顺序从oracle db表中获取记录

时间:2014-09-04 11:11:20

标签: sql oracle sql-order-by

我在oracle中有表,其中可以包含以下数据

W_ID        S_ID         SEQ      Type
1           Template1     1        W
1           Template2     2        W
1           1             3        S
Template1   2             1        S
Template1   3             2        S
Template2   4             1        S
Template2   5             2        S
Template2   6             3        S

现在我想通过SEQ得到W_Id = 1的S_ID 意味着我想在Template2

之后获取Template1的第一个结果

结果应如下:

S_ID  SEQ
2     1
3     2
4     1
5     2
6     3
1     3

我写了如下查询

select W_ID, S_ID, SEQ from Table1 where 

  W_ID in (select S_ID from Table1 where W_ID='1' and Type='W') 
  or 
  (S_ID in (select S_ID from Table1 where W_ID='1' and Type='S') and W_ID='1')

order by SEQ

现在问题是我无法按顺序获取结果 请帮忙,

Ragards, 约杰什

2 个答案:

答案 0 :(得分:0)

您似乎只需要自我加入即可获得所需的结果:

with table1(W_ID,S_ID,SEQ,Type) as (
  select '1', 'Template1', 1, 'W' from dual union all 
  select '1', 'Template2', 2, 'W' from dual union all 
  select '1', '1', 3, 'S' from dual union all 
  select 'Template1', '2', 1, 'S' from dual union all 
  select 'Template1', '3', 2, 'S' from dual union all 
  select 'Template2', '4', 1, 'S' from dual union all 
  select 'Template2', '5', 2, 'S' from dual union all 
  select 'Template2', '6', 3, 'S' from dual)
----------
-- End if Data
----------
select a.S_ID a_s_id, b.s_id b_s_id, b.seq 
  from table1 a
  join table1 b on a.s_id = b.w_id
 where a.w_id = '1'
   and (a.type='W' or a.w_id = b.s_id)
   and b.type='S'
 order by a.seq, b.s_id;

输出:

|    A_S_ID | B_S_ID | SEQ |
|-----------|--------|-----|
| Template1 |      2 |   1 |
| Template1 |      3 |   2 |
| Template2 |      4 |   1 |
| Template2 |      5 |   2 |
| Template2 |      6 |   3 |
|         1 |      1 |   3 |

更新

在更改序列后,它工作正常(从评论部分: - >“更改S_Id的序列= 1到2并使模板2的序列为3然后结果应该是template1的记录然后记录S_Id 1然后以这种方式记录template2

with table1(W_ID,S_ID,SEQ,Type) as (
      select '1', 'Template1', 1, 'W' from dual union all 
      select '1', 'Template2', 3, 'W' from dual union all 
      select '1', '1', 2, 'S' from dual union all 
      select 'Template1', '2', 1, 'S' from dual union all 
      select 'Template1', '3', 2, 'S' from dual union all 
      select 'Template2', '4', 1, 'S' from dual union all 
      select 'Template2', '5', 2, 'S' from dual union all 
      select 'Template2', '6', 3, 'S' from dual)
----------
-- End if Data
----------
select a.S_ID a_s_id, b.s_id b_s_id, b.seq 
  from table1 a
  join table1 b on a.s_id = b.w_id
 where a.w_id = '1'
   and (a.type='W' or a.w_id = b.s_id)
   and b.type='S'
 order by a.seq, b.s_id;

输出:

|    A_S_ID | B_S_ID | SEQ |
|-----------|--------|-----|
| Template1 |      2 |   1 |
| Template1 |      3 |   2 |
|         1 |      1 |   2 |
| Template2 |      4 |   1 |
| Template2 |      5 |   2 |
| Template2 |      6 |   3 |

答案 1 :(得分:0)

您希望先按模板seq排序,然后按个别类型排序' S'起。这可以通过加入:

select t1.w_id
     , case t1.type when 'W' then t2.s_id else t1.s_id end s_id
     , case t1.type when 'W' then t2.seq  else t1.seq  end seq
  from table1 t1
  left outer join table1 t2
      on t1.type = 'W'
      and t2.w_id = t1.s_id
      and t2.type = 'S'
 where t1.w_id = '1'
 order by t1.seq, t2.seq

要求的额外功能可以通过分析功能完成:

select w_id
     , s_id
     , seq
  from (
   select t1.w_id
        , case t1.type when 'W' then t2.s_id else t1.s_id end s_id
        , case t1.type when 'W' then t2.seq  else t1.seq  end seq
        , t1.seq t1seq
        , t2.seq t2seq
        , row_number() over (
             partition by
                t1.w_id
              , case t1.type when 'W' then t2.s_id else t1.s_id end
             order by
                t1.seq, t2.seq
          ) rn
     from table1 t1
     left outer join table1 t2
         on t1.type = 'W'
         and t2.w_id = t1.s_id
         and t2.type = 'S'
    where t1.w_id = '1'
  )
 where rn = 1
 order by t1seq, t2seq

可能有更简单的方法 - 这就是我能想到的快速; - )