如何将数字范围拆分为单个数字 Oracle

huangapple go评论98阅读模式
英文:

How to break number range to individual numbers oracle

问题

如何将数字范围拆分为单独的数字 Oracle

我的表中有以下数字范围

START_SN END_SN
850000 850003
850015 850017

并且需要以下结果

SN
850000
850001
850002
850003
850015
850016
850017
英文:

How to break number range to individual numbers oracle

I have following number ranges in my table

START_SN END_SN
850000 850003
850015 850017

And need following result.

SN
850000
850001
850002
850003
850015
850016
850017

答案1

得分: 0

以下是已翻译的部分:

样本表格:

SQL> SELECT * FROM number_ranges;

  START_SN     END_SN
---------- ----------
    850000     850003
    850015     850017

这是一种选项,可以返回所需的结果(并避免**重复**):

SQL>   SELECT start_sn + COLUMN_VALUE - 1 AS sn
  2      FROM number_ranges
  3           CROSS JOIN
  4           TABLE (
  5              CAST (
  6                 MULTISET (    SELECT LEVEL
  7                                 FROM DUAL
  8                           CONNECT BY LEVEL <= end_sn - start_sn + 1) AS SYS.odcinumberlist))
  9  ORDER BY sn;

        SN
----------
    850000
    850001
    850002
    850003
    850015
    850016
    850017

7 rows selected.

SQL>;
英文:

Sample table:

SQL&gt; SELECT * FROM number_ranges;

  START_SN     END_SN
---------- ----------
    850000     850003
    850015     850017

This is one option which returns desired result (and avoids duplicates):

SQL&gt;   SELECT start_sn + COLUMN_VALUE - 1 AS sn
  2      FROM number_ranges
  3           CROSS JOIN
  4           TABLE (
  5              CAST (
  6                 MULTISET (    SELECT LEVEL
  7                                 FROM DUAL
  8                           CONNECT BY LEVEL &lt;= end_sn - start_sn + 1) AS SYS.odcinumberlist))
  9  ORDER BY sn;

        SN
----------
    850000
    850001
    850002
    850003
    850015
    850016
    850017

7 rows selected.

SQL&gt;

答案2

得分: -1

要将一个数字范围在Oracle中拆分为单独的数字,您可以使用子查询、连接和CONNECT BY LEVEL子句等技巧的组合。以下是一个实现所需结果的示例查询:

WITH number_ranges AS (
  SELECT 850000 AS start_sn, 850003 AS end_sn FROM dual
  UNION ALL
  SELECT 850015 AS start_sn, 850017 AS end_sn FROM dual
)
SELECT start_sn + LEVEL - 1 AS sn
FROM number_ranges
CONNECT BY LEVEL <= (end_sn - start_sn + 1)
ORDER BY sn;

修改:
number_ranges 子查询用于表示您的表数据。您可以将其替换为您实际的表名。

英文:

To break a number range into individual numbers in Oracle, you can use a combination of techniques such as subqueries, joins, and the CONNECT BY LEVEL clause. Here's an example query that achieves the desired result:

WITH number_ranges AS (
  SELECT 850000 AS start_sn, 850003 AS end_sn FROM dual
  UNION ALL
  SELECT 850015 AS start_sn, 850017 AS end_sn FROM dual
)
SELECT start_sn + LEVEL - 1 AS sn
FROM number_ranges
CONNECT BY LEVEL &lt;= (end_sn - start_sn + 1)
ORDER BY sn;

Modifications:
The number_ranges subquery is used to represent your table data. You can replace it with your actual table name.

huangapple
  • 本文由 发表于 2023年6月5日 13:35:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定