Quantcast
Channel: Oracle
Viewing all articles
Browse latest Browse all 1814

Wiki Page: HOW TO USE ORDER BY CLAUSE INSIDE UNION ALL QUERY

$
0
0
HOW TO USE ORDER BY CLAUSE INSIDE UNION ALL QUERY (USING ORDER BY CLAUSE IN EACH INDIVIAUAL QUERIES & JOIN THEM USNING UNION ALL) Author JP Vijaykumar Date March 8th 2016 create table temp_jp(id number,name varchar2(20)); insert into temp_jp values(1,'Veeksha'); insert into temp_jp values(2,'Saketharama'); insert into temp_jp values(3,'Vineela'); commit; SQL> select * from temp_jp; ID NAME ---------- -------------------- 1 Veeksha 2 Vineela 3 Saketharama --Here I am using two select queries with ORDER BY clause and join them with a UNION ALL. SQL> select * from temp_jp where name like 'S%' order by 1 union all select * from temp_jp where name like 'V%' order by 1; 2 3 union all * ERROR at line 2: ORA-00933: SQL command not properly ended --I modified the query as show below and executed successfully. SQL> with t1 as (select * from temp_jp where name like 'S%' order by name), t2 as (select * from temp_jp where name like 'V%' order by name) select * from t1 union all select * from t2; 2 3 4 5 ID NAME ---------- -------------------- 2 Saketharama 1 Veeksha 3 Vineela --For the convenience of readability, I want to insert a blank line between the two queries. SQL> with t1 as (select * from temp_jp where name like 'S%' order by name), 2 t2 as (select * from temp_jp where name like 'V%' order by name) 3 select * from t1 4 union all 5 select null from dual --NEED TO INSERT A BLANK LINE INBETWEEN 6 union all 7 select * from t2; select null from dual --NEED TO INSERT A BLANK LINE INBETWEEN * ERROR at line 5: ORA-01789: query block has incorrect number of result columns --I need to select equal number of null values from DUAL, as the number of columns --were included in the other queries. SQL> with t1 as (select * from temp_jp where name like 'S%' order by name), 2 t2 as (select * from temp_jp where name like 'V%' order by name) select * from t1 union all 3 select null,null from dual --NEED TO SELECT EQUAL NUMBER OF NULL COLUMNS, AS WERE SELECTED IN OTHER QUERIES union all select * from t2; 4 5 6 7 ID NAME ---------- -------------------- 2 Saketharama 1 Veeksha 3 Vineela 3 rows selected. --Here I generated two sql quereis with ORDER BY cluse and joining with UNION ALL, --and seperated the two sql queries with a blank line. Happy scripting.

Viewing all articles
Browse latest Browse all 1814

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>