简体   繁体   中英

I want to write multiple queries results one after another in oracle

For ex : if there are 3 sql statements and it produces 2 rows per each sql.. it should display the result in below format

Row 1 from sql1
Row 1 from sql2
Row 1 from sql3
Row 2 from sql1
Row 2 from sql2
Row 2 from sql3

I tried union thinking it may help but it prints first sql statement results and goes to next one.

Any help would be highly appreciated

You can take advantage of ROWNUM here:

SELECT <COLUM_LIST> FROM
(SELECT <COLUM_LIST>, ROWNUM AS RN, 1 AS QUERY_NUM from table1 -- sql query 1
UNION ALL
SELECT <COLUM_LIST>, ROWNUM AS RN, 2 AS QUERY_NUM from table2 -- sql query 2
UNION ALL
SELECT <COLUM_LIST>, ROWNUM AS RN, 3 AS QUERY_NUM from table3 -- sql query 3
)
ORDER BY RN, QUERY_NUM

If you want to define specific order of record in each query then instead of ROWNUM , you can use ROW_NUMBER analytical function.

Cheers!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM