2

2 つの列の間の数値のリストを取得したい。テーブルの値は、より多くの行を生成するために使用されます。

例: 表 1:

Key StartNum EndNum
--- -------- ------
A   1        3
B   6        8

私の出力は次のようになります。

Key Num
--- ---
A   1
A   2
A   3
B   6
B   7
B   8

これを試しましたが、役に立ちませんでした(キーのある行が必要です)。

これを Oracle 11g で解決する必要があります。

4

4 に答える 4

1

a_horse_with_no_name-s ソリューションは

 SELECT distinct Key,(level + StartNum)-1 Num
   FROM Table1
  CONNECT BY (LEVEL +StartNum ) <= EndNum+1
  order by Key, Num

出力:

A   1                                     
A   2                                     
A   3                                     
B   6                                     
B   7                                     
B   8                                     

しかし、上記の方法にはテーブルに後続のデカルトが含まれているため、グローバル一時テーブルを作成してplsqlからデータを入力することをお勧めします(したがって、個別が必要です)。 http://www.dba-oracle.com/t_temporary_tables_sql.htm

于 2011-09-19T07:59:28.203 に答える
0

これは、投稿されたジャスティンのソリューションのわずかに適応したバージョンです: 2 つの列の間の数字のリストを取得する

select key, num 
from (
  select distinct t1.key, t1.startnum + level - 1 num, t1.startnum, t1.endnum
  from table1 t1
  connect by level <= (select t2.endnum from table1 t2 where t1.key = t2.key)
) t
where num between t.startnum and t.endnum
order by key, num

内部クエリの必要性には満足してdistinctいませんが、現在、これを深く掘り下げる時間はありません。

于 2011-09-19T07:07:04.053 に答える
0

これを試して、

SELECT t.StartNum , t.StartNum , ROWNUM
FROM Table1 t , ALL_OBJECTS
WHERE ROWNUM between t.StartNum  and t.StartNum 
于 2011-09-19T07:09:45.880 に答える
-1

TransactSQLでストアドプロシージャを作成する

Create Procedure GetRangeFromTable 
As
Begin 

    create table #Result(           
            code varchar(50),
            num int
    )  

    Declare 
     @code varchar(50),
     @start int ,
     @end int

    DECLARE num_cursor CURSOR FOR Select * from Table1
    OPEN num_cursor

    FETCH NEXT FROM num_cursor 
    INTO @code, @start, @end


    WHILE @@FETCH_STATUS = 0
    BEGIN

        While @start <= @end
        Begin
            Insert into #Result(code,num) Values (@code,@start)
            Set @start= @start + 1
        End 

       FETCH NEXT FROM num_cursor 
       INTO @code, @start, @end

    END

    Select * from #Result

    CLOSE num_cursor
    DEALLOCATE num_cursor

End 
于 2011-09-19T06:14:20.057 に答える