• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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