• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

PL/SQL select statement help - ordering by column not selected

I'm trying to select a distinct column from a table.  I want to order the result but not select the column I want to order by.  I can't find a way to do it.

e.g.

Table1
Col1   Col2
1        colvalue2
2        colvalue2
3        colvalue2
4        colvalue1
5        colvalue1
6        colvalue1
7        colvalue3
8        colvalue3

So, if I do a standard select statement.
Select distinct col2 from table1
I'll get the result
colvalue1
colvalue2
colvalue3

i.e. by default it's ordering alphabetically.

How do I order the result by Col1 in table 1, but still only return the distinct values?  I've tried a few different things like using sub selects and using rownum etc.... can't figure it out.  Appreciate the help.....

Wanted result:
colvalue2
colvalue1
colvalue3
0
obrienj
Asked:
obrienj
1 Solution
 
SharathData EngineerCommented:
try this.
Select col2 from table1 group by col2 order by max(col1)

Open in new window

0
 
obrienjAuthor Commented:
Thanks for quick response.... works perfectly.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now