Skip to content
Advertisement

How to merge two result columns into column with alternating results in oracle?

For a table with columns A and B, select * from table will give back

A        B
A1      B1
A2      B2
A3      B3

Is there a way to query on this so that the result set is like this –

Merged
A1
B1
A2
B2
A3
B3

Appreciate any suggestions 🙂

Advertisement

Answer

A union with some added spice to determine ordering would work:

SELECT col
FROM (
  SELECT 'A' as colname, row_number() OVER (ORDER BY A) as colrow, A as col FROM table
  UNION ALL
  SELECT 'B' as colname, row_number() OVER (ORDER BY B) as colrow, B as col FROM table
) sub
ORDER BY colrow, colname
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement