根据单元格中的值创建记录

时间:2019-06-06 13:49:51

标签: sql oracle

我有一个表,该表提供了总销售数量的概述,并且我需要基于这些值创建一个包含单个(重复)记录的表。

例如参见表1

| Date   | ID   | Metric       | Response | Value |
|--------|------|--------------|----------|-------|
| Feb 19 | abc1 | Sell Product | Yes      | 3     |
| Feb 19 | abc1 | Sell Product | Total    | 4     |

1 个答案:

答案 0 :(得分:0)

这就是我对问题的理解:

SQL> with table1 (c_date, id, metric, response, value) as
  2    (select 'Feb 19', 'abc1', 'Sell Product', 'Yes'  , 3 from dual union all
  3     select 'Feb 19', 'abc1', 'Sell Product', 'Total', 4 from dual
  4    )
  5  select c_date, id, metric, response, value
  6  from table1 join table(cast(multiset(select level from dual
  7                                       connect by level <= value
  8                                      ) as sys.odcinumberlist)) on 1 = 1;

C_DATE ID   METRIC       RESPO      VALUE
------ ---- ------------ ----- ----------
Feb 19 abc1 Sell Product Yes            3
Feb 19 abc1 Sell Product Yes            3
Feb 19 abc1 Sell Product Yes            3
Feb 19 abc1 Sell Product Total          4
Feb 19 abc1 Sell Product Total          4
Feb 19 abc1 Sell Product Total          4
Feb 19 abc1 Sell Product Total          4

7 rows selected.

SQL>

(在此示例中,日期是 strings ,但这并不重要-如果您可以接受总体思路)。