2013-01-02 34 views
2

我有一個包含以下內容的表:使用Oracle 11g生成數範圍之間的後續數字與SQL

ID low_value  high_value 
1  3270200000 3270210000 
2  3270210000 3270220000 
3  3270220000 3270230000 
4  3270230000 3270231000 
5  3270231000 3270232000 
6  3270232000 3270240000 
... 

在一個單一的查詢,我想下面的檢索結果,其中我列出存在每一個獨特的編號與

start value = with low_value/
end value = high_value - 1 

 

low_value high_value unique_value 
3270200000 3270210000 3270200000  
3270200000 3270210000 3270200001 
3270200000 3270210000 3270200002 
3270200000 3270210000 3270200003 
... 
3270200000 3270210000 3270209999 
3270210000 3270220000 3270210001 
3270210000 3270220000 3270210002 
3270210000 3270220000 3270210002 
... 
3270210000 3270220000 3270219999 
... 

我定義的範圍內一直在玩連接&模型條款,但迄今沒有成功。

TX使用示範條款你的幫助

+0

你可以發佈你嘗試過什麼? – Mari

回答

5

SQL> create table foo1 
    2 (id number, 
    3 low_value number, 
    4 high_value number); 

Table created. 

SQL> 
SQL> insert into foo1 values (1, 3270200000, 3270200010); 

1 row created. 

SQL> insert into foo1 values (2, 3270210000, 3270210005); 

1 row created. 

SQL> insert into foo1 values (3, 10, 10); 

1 row created. 

SQL> commit; 

Commit complete. 

SQL> 
SQL> 
SQL> with foo as 
    2 (select f.id, f.low_value, f.high_value, f.high_value - f.low_value 
    3  range from foo1 f) 
    4 select key, low_value, high_value, unique_value 
    5 from foo 
    6 model partition by(id as key) 
    7   dimension by(0 as f) 
    8   measures(low_value as unique_value, low_value, high_value, range) 
    9   rules (unique_value [for f from 0 to range[0] increment 1] = low_value[0] + cv(f), 
10    low_value[for f from 0 to range[0] increment 1] = low_value[0], 
11    high_value[for f from 0 to range[0] increment 1] = high_value[0]); 

     KEY LOW_VALUE HIGH_VALUE UNIQUE_VALUE 
---------- ---------- ---------- ------------ 
     1 3270200000 3270200010 3270200000 
     1 3270200000 3270200010 3270200001 
     1 3270200000 3270200010 3270200002 
     1 3270200000 3270200010 3270200003 
     1 3270200000 3270200010 3270200004 
     1 3270200000 3270200010 3270200005 
     1 3270200000 3270200010 3270200006 
     1 3270200000 3270200010 3270200007 
     1 3270200000 3270200010 3270200008 
     1 3270200000 3270200010 3270200009 
     1 3270200000 3270200010 3270200010 
     2 3270210000 3270210005 3270210000 
     2 3270210000 3270210005 3270210001 
     2 3270210000 3270210005 3270210002 
     2 3270210000 3270210005 3270210003 
     2 3270210000 3270210005 3270210004 
     2 3270210000 3270210005 3270210005 
     3   10   10   10 

18 rows selected. 

SQL> 

和11g遞歸保

SQL> with foo (id, low_value, high_value, unique_value) 
    2  as (select f.id, f.low_value, f.high_value, low_value unique_value 
    3    from foo1 f 
    4    union all 
    5   select id, low_Value, high_value, unique_value + 1 
    6    from foo 
    7    where unique_value < high_value) 
    8 select id, low_value, high_value, unique_value 
    9 from foo 
10 order by id, unique_value 
11/

     ID LOW_VALUE HIGH_VALUE UNIQUE_VALUE 
---------- ---------- ---------- ------------ 
     1 3270200000 3270200010 3270200000 
     1 3270200000 3270200010 3270200001 
     1 3270200000 3270200010 3270200002 
     1 3270200000 3270200010 3270200003 
     1 3270200000 3270200010 3270200004 
     1 3270200000 3270200010 3270200005 
     1 3270200000 3270200010 3270200006 
     1 3270200000 3270200010 3270200007 
     1 3270200000 3270200010 3270200008 
     1 3270200000 3270200010 3270200009 
     1 3270200000 3270200010 3270200010 

     ID LOW_VALUE HIGH_VALUE UNIQUE_VALUE 
---------- ---------- ---------- ------------ 
     2 3270210000 3270210005 3270210000 
     2 3270210000 3270210005 3270210001 
     2 3270210000 3270210005 3270210002 
     2 3270210000 3270210005 3270210003 
     2 3270210000 3270210005 3270210004 
     2 3270210000 3270210005 3270210005 
     3   10   10   10 

18 rows selected. 

SQL> 
+0

謝謝。這兩個工作正常。 – tijz

+0

+1對於廣泛的價值觀,這將是更有效的方法。 –