如何将数字范围分解为单个数字oracle [重复]

68de4m5k  于 2023-06-22  发布在  Oracle
关注(0)|答案(2)|浏览(122)

此问题已在此处有答案

Generate numbers for given ranges (sql query)(4个答案)
16天前关闭
如何将数字范围分解为单个数字oracle
我的表中有以下数字范围
| 开始_序列号|END_SN|
| - -----|- -----|
| 八十五万|850003|
| 850015| 850017|
并需要以下结果。
| SN|
| - -----|
| 八十五万|
| 850001|
| 850002|
| 850003|
| 850015|
| 850016|
| 850017|

nwwlzxa7

nwwlzxa71#

样品表:

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>
dm7nw8vv

dm7nw8vv2#

要在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子查询用于表示表数据。您可以将其替换为实际的表名。

相关问题