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

Posted on 2011-05-12
Last Modified: 2013-12-07
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.


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

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:
Question by:obrienj
    LVL 40

    Accepted Solution

    try this.
    Select col2 from table1 group by col2 order by max(col1)

    Open in new window


    Author Closing Comment

    Thanks for quick response.... works perfectly.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Able to SELECT but not DELETE 4 36
    Read only access to a user 5 83
    Oracle SQL Sub Query 5 41
    Space Delimited Sql File 4 59
    This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
    I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
    This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now