使用 Connect by level 复制行

Duplicate rows using Connect by level

我希望使用 CONNECT BY LEVEL 根据列值复制行。

这是我使用的代码:

SELECT 
    tbl1.*,
    (tbl1.START_WEEK - tbl1.END_WEEK) wks_inbtwn

FROM
    My_SQL_table tbl1 INNER JOIN (SELECT rownum repeat FROM dual CONNECT BY LEVEL <= tbl1.END_WEEK ) tbl2
    ON tbl2.Repeat > tbl1.START_WEEK

我不断收到错误消息:

SQL Error [904] [42000]: ORA-00904: "tbl1"."START_WEEK": invalid identifier

我的table如下:

+------------+------------+----------+
|  Site_NUM  | start_week | end_week |
+------------+------------+----------+
| France     |         50 |       52 |
| Germany    |         41 |       43 |
| USA        |         12 |       13 |
+------------+------------+----------+

我想要的结果如下:

+----------+---------+
| Site_NUM | Week_no | 
+----------+---------+
| France   |      51 |
| France   |      52 |
| Germany  |      42 |
| Germany  |      43 |
| USA      |      13 |
+----------+---------+

任何帮助将不胜感激,提前致谢。

Oracle 设置:

CREATE TABLE My_SQL_table ( Site_NUM, start_week, end_week ) AS
SELECT 'France',  50, 52 FROM DUAL UNION ALL
SELECT 'Germany', 41, 43 FROM DUAL UNION ALL
SELECT 'USA',     12, 13 FROM DUAL;

查询:使用CONNECT BY

SELECT site_num,
       COLUMN_VALUE wks_inbtwn
FROM   My_SQL_table tbl1
       CROSS JOIN 
       TABLE(
         CAST(
           MULTISET(
             SELECT tbl1.START_WEEK + LEVEL
             FROM   DUAL
             CONNECT BY tbl1.START_WEEK + LEVEL <= tbl1.END_WEEK
           )
           AS SYS.ODCINUMBERLIST
         )
       )

输出:

SITE_NUM | WKS_INBTWN
:------- | ---------:
France   |         51
France   |         52
Germany  |         42
Germany  |         43
USA      |         13

查询 2:使用递归 sub-query 分解子句

WITH rsqfc ( site_num, start_week, end_week ) AS (
  SELECT site_num, start_week + 1, end_week
  FROM   my_sql_table
UNION ALL
  SELECT site_num, start_week + 1, end_week
  FROM   rsqfc
  WHERE  start_week < end_week
)
SELECT site_num, start_week AS wks_inbtwn
FROM   rsqfc
ORDER BY site_num, wks_inbtwn

输出:

SITE_NUM | WKS_INBTWN
:------- | ---------:
France   |         51
France   |         52
Germany  |         42
Germany  |         43
USA      |         13

db<>fiddle here