1. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

sql: distinct - & column sequence

Discussion in 'SQL PL/SQL' started by monkey, Sep 2, 2014.

  1. monkey

    monkey Long Term Member

    Messages:
    156
    Likes Received:
    0
    Trophy Points:
    230
    Hello!
    Here is the example from the tutorial beginning.

    http://www.w3schools.com/sql/sql_distinct.asp

    I wrote :
    SELECT distinct city, CustomerID FROM [Customers]

    and U all know what I got.

    But then I came to the ideal, that CustomerID column should be first, and the city column second.
    My question is how to achieve it, tried to google, but had no idea how to name the problem in english.

    p.s I tried like:
    SELECT distinct CustomersID, city FROM [Customers]


    and
    SELECT distinct (CustomersID, city) FROM [Customers]
    ..but did tno help.


    Many thanks!!
     
  2. jagadekara

    jagadekara Forum Guru

    Messages:
    1,126
    Likes Received:
    145
    Trophy Points:
    1,805
    Location:
    Pulivendula
    Hi,

    What's the problem you are facing?

    SELECT distinct CustomersID, city FROM [Customers]

    In above query you used CustomersID but actually it is CustomerID

    So try this....

    SELECT distinct CustomerID, city FROM [Customers]
     
  3. rajenb

    rajenb Forum Expert

    Messages:
    361
    Likes Received:
    114
    Trophy Points:
    655
    Location:
    Mauritius
    Hi "monkey",

    The only difference I can see between the 2 queries is that the way the output is sorted: always by the first column.

    If you run "SELECT distinct CustomerID , city FROM Customers;" and want it sorted by city like in the output of query "SELECT distinct city, CustomerID FROM Customers;", then you just have to add an ORDER BY in the first statement, i.e.,

    Code (SQL):
    SELECT DISTINCT CustomerID , city FROM Customers
    ORDER BY city;