2012-03-30 72 views
0

我想用一個相同的自然鍵組中的空值排序連續的行。數據庫是oracle。如何在查詢中用空值排序順序行?

這裏的例子:

NAT_KEY  ATTRIBUTE_A  ORDERED_FIELD    RANK 
    A    A      1 
    A          2     1 
    A    A      4 
    A    I      6 
    A          8     1 
    A          10     2 
    A          11     3 
    B          2     1 
    B          3     2 
    B    A      5 
    B    A      6 
    B          9     1 
    C    A      1 
    C    A      5 
    C    I      6 
    C          7     1 
    C          8     2 

必須有與row_number()功能,levelconnect by或另一個方式。

+1

相似:[基於來自多行SQL Server的標誌的時間總和](http://stackoverflow.com/questions/8046386/sum-of-time-based-on-flag-from-multiple-rows-sql -服務器)。 – 2012-03-30 14:14:32

回答

0

輸出我終於想通了與舍甫琴科M的鏈接。 下面是解決方案:

select grouped_table.nat_key, 
     grouped_table.attr, 
     grouped_table.order_by, 
     case 
     when attr is null 
      then row_number() over (partition by nat_key, attr, grp order by order_by) 
     else null 
     end rowrank 
from (
    select the_table.*, 
     row_number() over (partition by nat_key order by order_by) - row_number() over (partition by nat_key, nvl2(attr, 1, 0) order by order_by) grp 
    from (
    select 'A' nat_key, 'A' attr, 1 order_by from dual 
    union all 
    select 'A' nat_key, null attr, 2 order_by from dual 
    union all 
    select 'A' nat_key, 'A' attr, 4 order_by from dual 
    union all 
    select 'A' nat_key, 'I' attr, 6 order_by from dual 
    union all 
    select 'A' nat_key, null attr, 8 order_by from dual 
    union all 
    select 'A' nat_key, null attr, 10 order_by from dual 
    union all 
    select 'A' nat_key, null attr, 11 order_by from dual 
    union all 
    select 'B' nat_key, null attr, 2 order_by from dual 
    union all 
    select 'B' nat_key, null attr, 3 order_by from dual 
    union all 
    select 'B' nat_key, 'A' attr, 5 order_by from dual 
    union all 
    select 'B' nat_key, 'A' attr, 6 order_by from dual 
    union all 
    select 'B' nat_key, null attr, 9 order_by from dual 
    union all 
    select 'C' nat_key, 'A' attr, 1 order_by from dual 
    union all 
    select 'C' nat_key, 'A' attr, 5 order_by from dual 
    union all 
    select 'C' nat_key, 'I' attr, 6 order_by from dual 
    union all 
    select 'C' nat_key, null attr, 7 order_by from dual 
    union all 
    select 'C' nat_key, null attr, 8 order_by from dual 
) the_table 
) grouped_table 
order by nat_key, order_by 

謝謝!

0

我的猜測是,你想要的東西像

select key, 
     attr, 
     order_by, 
     (case when rnk1 is not null 
      then rank() over (partition by key order by rnk1) 
      else null 
     end) rnk 
    from (
    select x.*, 
      (case when attr is null 
       then row_number() over (partition by key order by order_by) 
       else null 
      end) rnk1 
     from <<table name>> x 
) 
order by key, order_by 

產生這樣

SQL> ed 
Wrote file afiedt.buf 

    1 with x as (
    2 select 'A' key, 'A' attr, 1 order_by from dual union all 
    3 select 'A', null, 2 from dual union all 
    4 select 'A', 'A', 4 from dual 
    5 ) 
    6 select key, 
    7   attr, 
    8   order_by, 
    9   (case when rnk1 is not null 
10    then rank() over (partition by key order by rnk1) 
11    else null 
12   end) rnk 
13 from (
14  select x.*, 
15    (case when attr is null 
16     then row_number() over (partition by key order by order_by) 
17     else null 
18    end) rnk1 
19  from x 
20 ) 
21* order by key, order_by 
SQL>/

K A ORDER_BY  RNK 
- - ---------- ---------- 
A A   1 
A   2   1 
A A   4 
+0

如果您嘗試完整示例,則會看到它不起作用。不管怎麼說,還是要謝謝你。 – codea 2012-03-30 12:11:20