sql - Union and order by a column not selected -
let's have table a, columns a_1 , a_2.
i interested in column a_1
, can write :
select a_1 a;
however want ordered column a_2
, write :
select a_1 order a_2 desc;
now want union :
select a_1 <some filters> union select a_1 <some other filters> order a_2 desc;
this doesn't work anymore ("a_2": invalid identifier
), change :
select a_1, a_2 <some filters> union select a_1, a_2 <some other filters> order a_2 desc;
the select a_2
part bothers me : don't want show in results, how can hide information ?
conceptually, doesn't make sense order a_2
after you've done union
on a_1
. oracle's perspective, entirely possible distinct a_1
value in either query, there may many different a_2
values returned. there many possible sort orders.
you can, of course, nest query a_1
in results
select a_1 (select a_1, a_2 <some filters> union select a_1, a_2 <some other filters>) sub_q order a_2 desc;
of course, performance standpoint, make sense use union all
rather union
unless want , eliminate duplicate rows. , if both queries pulling same table, makes more sense use single query or
rather writing union
of 2 different queries.
Comments
Post a Comment