根据列的值在结果中重复行

sak*_*vel 1 oracle

FIRST_COL VAL_COL
A          2 
B          3
C         4
Run Code Online (Sandbox Code Playgroud)

我需要如下输出。

FIRST_COL VAL_COL
A          2
A          2
B          3
B          3
B          3
C          4
C          4
C          4
C          4
Run Code Online (Sandbox Code Playgroud)

示例:我的原始表包含a=2但我需要在输出中作为a=2b=3 三次 c=4 四次的两倍

Glo*_*del 5

如果您使用的是 Oracle 12c 或更高版本,则可以CROSS APPLY使用CONNECT BY LEVEL查询生成从 1 到VAL_COL. 像这样的东西(我没有准备好实例,SQLFiddle 使用 11g,它不支持这个):

SELECT FIRST_COL, VAL_COL
FROM table1
CROSS APPLY (
  SELECT LEVEL
    FROM DUAL  
    CONNECT BY LEVEL <= table1.VAL_COL
);
Run Code Online (Sandbox Code Playgroud)

如果您使用 11g 或更高版本,则可以使用Recursive Refactored Subquery.

with data as (
  -- example data
  select 'A' FIRST_COL, 2 VAL_COL from dual union all
  select 'B' FIRST_COL, 3 VAL_COL from dual union all
  select 'C' FIRST_COL, 4 VAL_COL from dual
), counter(first_col,val_col, iterator) as (
  select first_col, val_col, val_col
  from data
  union all
  select first_col, val_col, iterator - 1
  from counter
  where iterator - 1 > 0
)
select first_col, val_col
from counter
order by first_col;
Run Code Online (Sandbox Code Playgroud)

在 11g 之前,您需要使用 pipeline function