Google
Information Storage and Retrieval: June 2012

Pages

Tuesday, June 5, 2012

Exploding Rows........

SQL Puzzle:


You have a table with 2 columns: literal and num with data as shown below:


Literal     NUM
 a                 4
 b                 3
 c                 2


Write a query that returns the output as:


Literal
----------
   a
   a
   a
   a
   b
   b
   b
   c
   c


The NUM represents the number of times the literal has to be repeated in the output




Solution:


The query will be as follows:


select a.literal
from
(select literal, num from explode) a, (select rownum rn from user_objects) b
where b.rn<=a.num