I have one table, call it sach_temp, with below sample data.

TO_VALUE FROM_VALUE DATE_1

200      100        2/13/2014
238      234        2/13/2014

and I want to generate output like below

VALUE   DATE_1
100     2/13/2014
101     2/13/2014
102     2/13/2014
103     2/13/2014
.
.
200     2/13/2014
234     2/13/2014
.
.
238     2/13/2014

I can think of something like below but it will be working only for single row in table. Can someone guide me how to handle this?

   SELECT from_value+rownum-1 value, date_1
     FROM 
    (SELECT     1 JUST_A_COL
          FROM DUAL
    CONNECT BY LEVEL <= (SELECT to_value-from_value+1 FROM sach_temp WHERE to_value=200)) a, sach_temp b
  WHERE to_value=200 
有帮助吗?

解决方案 2

Say the maximum difference between two values is 500. Then you can do this by creating a table with numbers and using join. Here is an example:

with nums as (
      select level as lvl from dual
      connect by level < 500
     )
select (st.from_value + n.lvl - 1) as value, st.date_1
from sach_temp st join
     nums n
     on st.from_value + n.lvl - 1<= st.to_value;

EDIT:

You don't have to guess at the maximum value. You can calculate that:

with vmax as (
      select max(to_value - from_value + 1) as vmax
      from sach_temp
     ),
     nums as (
      select level as lvl from vmax
      connect by level < vmax.vmax
     )
select (st.from_value + n.lvl - 1) as value, st.date_1
from sach_temp st join
     nums n
     on st.from_value + n.lvl - 1<= st.to_value;

其他提示

Provided your Oracle version is 11g or higher, you can use a recursive with clause and the query is simply:

with gen(max_num, min_num, value) as
(
  select max_num, min_num, value from mytable
  union all
  select max_num, min_num + 1, value from gen where min_num < max_num
)
select min_num as num, value 
from gen
order by min_num;

SQL fiddle: http://www.sqlfiddle.com/#!4/fc2c4/3.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top