I'm thinking this is an easy query but I don't know how to start. I have a database table called Booklet
with columns id
, controlNoFrom
and controlNoTo
. Sample data looked like this:
id | controlNoFrom | controlNoTo
---------+---------------+-------------
20150105 | 1 | 100
20150106 | 101 | 150
20150107 | 151 | 225
---------+---------------+-------------
What I want is a query that can present the data like this:
id | controlNo
---------+-----------
20150105 | 1
20150105 | 2
20150105 | 3
... | ...
20150105 | 99
20150105 | 100
20150106 | 101
20150106 | 102
... | ...
20150106 | 149
20150106 | 150
20150107 | 151
20150107 | 152
... | ...
20150107 | 224
20150107 | 225
---------+-----------
It's easy if it is the other way around, but I don't know what kind of SQL to be used in this scenario. What would you call this approach and what would the SQL statement in OracleDB look like?
Best Answer
You could use some kind of row generation technique, for example:
This generates as many rows as the largest range requires, with data
0, 1, 2, ...
, starting with0
, so no correction needed, when we add these values tocontrolnofrom
. Now simply join this to your original table and limit the number of rows bycontrolnoto - controlnofrom
, finally add the row number tocontrolnofrom
: