Skip to Main Content
  • Questions
  • Generate number based on start and end columns.

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Swapan.

Asked: March 20, 2019 - 7:31 am UTC

Last updated: March 20, 2019 - 1:52 pm UTC

Version: 10.1.1

Viewed 1000+ times

You Asked

Generate value based on start and end columns without using procedure.
How to modify the select query.

select key_column, start_point, end_point FROM tab1 WHERE key_column='10254';
key_column start_point end_point
10254 -2 5

Expected output:-
key_column points
10254 -2
10254 -1
10254 0
10254 1
10254 2
10254 3
10254 4
10254 5

and Chris said...

Looks like a standard group by to me...

with rws as (
  select 10254 ky, -2 val from dual union all
  select 10254 ky, -1 val from dual union all
  select 10254 ky, 0 val from dual union all
  select 10254 ky, 1 val from dual union all
  select 10254 ky, 2 val from dual union all
  select 10254 ky, 3 val from dual union all
  select 10254 ky, 4 val from dual union all
  select 10254 ky, 5 val from dual
)
  select ky, min ( val ), max ( val ) 
  from   rws
  group  by ky;

KY      MIN(VAL)   MAX(VAL)   
  10254         -2          5 

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Swapan Soren, March 20, 2019 - 11:24 am UTC

I need output like below :-
key_column points
10254      -2
10254      -1
10254       0
10254       1
10254       2
10254       3
10254       4
10254       5 

Chris Saxon
March 20, 2019 - 1:52 pm UTC

It helps if you provide the create table + inserts for your data...

Maybe like this

AndyP, March 20, 2019 - 12:01 pm UTC

with data as
(
select 10254 keycol,-2 start_point,5 end_point from dual union all
select 10255 keycol,-8 start_point,1 end_point from dual
)
select keycol
      ,start_point + rws.column_value - 1 datapoint
  from data
      ,table(cast(multiset
(
select level
  from dual
connect by level <= end_point-start_point + 1) as sys.odcinumberlist)) rws
/

    KEYCOL  DATAPOINT
---------- ----------
     10254         -2
     10254         -1
     10254          0
     10254          1
     10254          2
     10254          3
     10254          4
     10254          5
     10255         -8
     10255         -7
     10255         -6
     10255         -5
     10255         -4
     10255         -3
     10255         -2
     10255         -1
     10255          0
     10255          1

Chris Saxon
March 20, 2019 - 1:52 pm UTC

Nice stuff.

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.