使用SQL将输入表转换为提到的输出表

时间:2018-05-02 19:27:43

标签: sql oracle

我想通过使用SQL语句将输入表转换为提到的输出表,任何人都可以帮助我。

输入表

+-------------+-----------+----------+
| start_value | end_value | interval |
+-------------+-----------+----------+
|           0 |       120 |       10 |
|           1 |       150 |       50 |
+-------------+-----------+----------+

输出

+-----------------+-----------+------------------+
|     start_value | end_value | next_start_value |
+-----------------+-----------+------------------+
|               0 |       120 |                0 |
|               0 |       120 |               10 |
|               0 |       120 |               20 |
|               0 |       120 |               30 |
+-----------------+-----------+------------------+

2 个答案:

答案 0 :(得分:1)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE input ( start_value, end_value, "INTERVAL" ) AS
SELECT 0, 120, 10 FROM DUAL UNION ALL
SELECT 1, 150, 50 FROM DUAL;

查询1

WITH output ( rn, start_value, end_value, "INTERVAL", next_start_value ) AS (
  SELECT ROWNUM, i.*, start_value FROM input i
UNION ALL
  SELECT rn,
         start_value,
         end_value,
         "INTERVAL",
         next_start_value + "INTERVAL"
  FROM   output
  WHERE  next_start_value + "INTERVAL" <= end_value
)
SELECT start_value, end_value, next_start_value
FROM   output
ORDER BY rn, next_start_value

<强> Results

| START_VALUE | END_VALUE | NEXT_START_VALUE |
|-------------|-----------|------------------|
|           0 |       120 |                0 |
|           0 |       120 |               10 |
|           0 |       120 |               20 |
|           0 |       120 |               30 |
|           0 |       120 |               40 |
|           0 |       120 |               50 |
|           0 |       120 |               60 |
|           0 |       120 |               70 |
|           0 |       120 |               80 |
|           0 |       120 |               90 |
|           0 |       120 |              100 |
|           0 |       120 |              110 |
|           0 |       120 |              120 |
|           1 |       150 |                1 |
|           1 |       150 |               51 |
|           1 |       150 |              101 |

答案 1 :(得分:0)

由于MTD&#39; answer实际上并没有完全达到预期的输出结构,基于他/她的方法,我建议改变这个答案:

WITH output (start_value, end_value, "INTERVAL", next_start_value) AS (
  SELECT start_value, end_value, "INTERVAL", start_value AS next_start_value
    FROM input
UNION ALL
  SELECT start_value,
         end_value,
         "INTERVAL",
         next_start_value + "INTERVAL" AS next_start_value
    FROM output
   WHERE next_start_value + "INTERVAL" <= end_value
)
SELECT start_value, end_value, next_start_value
  FROM output
 ORDER BY 1, 2, 3;

编辑:添加查询结果:

START_VALUE | END_VALUE | NEXT_START_VALUE
------------+-----------+-----------------
          0 |       120 |                0
          0 |       120 |               10
          0 |       120 |               20
          0 |       120 |               30
          0 |       120 |               40
          0 |       120 |               50
          0 |       120 |               60
          0 |       120 |               70
          0 |       120 |               80
          0 |       120 |               90
          0 |       120 |              100
          0 |       120 |              110
          0 |       120 |              120
          1 |       150 |                1
          1 |       150 |               51
          1 |       150 |              101

This is the SQL Fiddle for it.