How to use UNION ALL, paging and adding another columns?
534754Jul 19 2007 — edited Jul 20 2007Hi everybody,
I would like to retrieve data from two tables using UNION ALL, then to use paging and then to add another columns.
I have this SELECT statement:
SELECT id, flag
FROM
(
SELECT /*+ FIRST_ROWS(10) */ id, flag, ROWNUM AS rn
FROM
(
SELECT * FROM
( SELECT id, 1 as flag, scale -- comment1
FROM table1
WHERE something1
UNION ALL
SELECT id1 as id, 0 as flag, scale -- comment2
FROM table2
WHERE something2
)
ORDER BY scale
)
)
WHERE rn BETWEEN 15 AND 24;
How can I add other columns into this result? But columns are different in table1 and table2.
For example, I have to add NULL as col1, col2, col3 from table1 and then col1, NULL as col2, col3 from table2.
I can do this by adding these columns where 'comment1' and 'comment2' are, but when I have to add more columns then three and when there is a lot of rows in the result, the SELECT statement is slow. So I assume that getting only IDs (and other necessary columns) and than paging the result and then add another columns will be faster than getting all columns and then paging this huge result.
Am I right?
So is this possible to do this?
Thanks all,
Matus