The Impaler
The Impaler

Reputation: 48770

Sorting rows without showing sorting column

In the query shown below, I need to show the first column N sorting by the second column priority, without showing the latter.

with number (n, priority) as (
  select 1, 12 from sysibm.sysdummy1 -- recursive complex query
) 
select n, priority from number
union all 
select 5, 13 from sysibm.sysdummy1 -- complex query here

The expected result is:

   N
   -
   1
   5

I can't really modify the query (since the real one is quite complex) and was wondering if I could enclose it as a subquery or maybe use it as a CTE.

Upvotes: 1

Views: 57

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269643

Use a subquery?

with number (n, priority) as (
  select 1, 12 from sysibm.sysdummy1 -- recursive complex query
) 
select n
from (select n, priority from number
      union all 
      select 5, 13 from sysibm.sysdummy1
     ) x
order by priority;

Upvotes: 2

Related Questions