Sql 我有一个数据如下的源表

Sql 我有一个数据如下的源表,sql,oracle,Sql,Oracle,但我需要以以下格式显示数据:- **COLOR** **TIMES** ORANGE 1 RED 2 BLACK 3 YELLOW 4 请向我推荐oracle SQL中的查询一个可能的解决方案: **COLOR** **TIMES** ORANGE 1 RED 1 RED 1 BLACK 1 BLACK 1 B

但我需要以以下格式显示数据:-

**COLOR**   **TIMES**
ORANGE         1
RED            2
BLACK          3
YELLOW         4
请向我推荐oracle SQL中的查询

一个可能的解决方案:

**COLOR**   **TIMES**
ORANGE          1
RED             1
RED             1
BLACK           1
BLACK           1
BLACK           1
YELLOW          1
YELLOW          1
YELLOW          1
YELLOW          1

也许不是很好,但它正在发挥作用:

SQL> with t as (
  2     select 'ORANGE' as color, 1 as times from dual
  3     union all select 'RED' as color, 2 as times from dual
  4     union all select 'BLACK' as color, 3 as times from dual
  5     union all select 'YELLOW' as color, 4 as times from dual),
  6  num as (
  7     select rownum as n
  8       from dual
  9    connect by level <= 4)
 10  select t.color,
 11         1 as times
 12    from t
 13    join num on num.n <= t.times
 14   order by t.times;

COLOR       TIMES
------ ----------
ORANGE          1
RED             1
RED             1
BLACK           1
BLACK           1
BLACK           1
YELLOW          1
YELLOW          1
YELLOW          1
YELLOW          1

10 rows selected.
WITH t AS
    (SELECT 'Orange' AS color, 1 AS times FROM dual UNION ALL
    SELECT 'Red' AS color, 2 AS times FROM dual UNION ALL
    SELECT 'Black' AS color, 3 AS times FROM dual UNION ALL
    SELECT 'Yellow' AS color, 4 AS times FROM dual), 
t2 AS 
    (SELECT DISTINCT color, 1 AS item, times, LEVEL AS C
    FROM t
    CONNECT BY LEVEL <= times)
SELECT color, item
FROM t2;