使用 key 获取两列之间的数字列表

发布于 2024-12-05 09:37:34 字数 410 浏览 1 评论 0原文

我想获取两列之间的数字列表。表值将用于生成更多行。

例如 表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 中解决这个问题。

I want to get the list of numbers in between two columns. A tables values will be used to generate more rows.

e.g
Table1:

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

My output should be:

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

I tried this, but it didn't help me (I need rows with the key).

I need to solve this in Oracle 11g.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁把谁当真 2024-12-12 09:37:34

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 填充它,因为上述方法包含表上的后续 decart(因此需要不同)。
http://www.dba-oracle.com/t_temporary_tables_sql.htm

a_horse_with_no_name-s solution would be

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

Output:

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

But I'd prefer creating a global temporary table and populate it from plsql, as the above method contains subsequent decarts on the table (thus the distinct required).
http://www.dba-oracle.com/t_temporary_tables_sql.htm

信仰 2024-12-12 09:37:34

这是贾斯汀解决方案的稍微改编版本,发布于: 获取两列之间的数字列表

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 不满意,但我目前没有时间更深入地研究这一点。

This is a slightly adapted version of Justin's solution posted in: get list of numbers in between two columns

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

I'm not happy with the need for the distinct in the inner query, but I currently don't have the time to dig deeper into this.

泪痕残 2024-12-12 09:37:34

试试这个,

SELECT t.StartNum , t.StartNum , ROWNUM
FROM Table1 t , ALL_OBJECTS
WHERE ROWNUM between t.StartNum  and t.StartNum 

Try this,

SELECT t.StartNum , t.StartNum , ROWNUM
FROM Table1 t , ALL_OBJECTS
WHERE ROWNUM between t.StartNum  and t.StartNum 
飞烟轻若梦 2024-12-12 09:37:34

在事务 SQL 中创建存储过程

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 

Create a store procedure in transact SQL

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文