Results 1 to 7 of 7
  1. #1
    hooty is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    4

    Select Distinct and Order By something else.

    Hello my friends
    I need some help with a query.

    I’ve got a table and like to select distinct coll prod_number and order my output by coll id autonumber.

    I know it won’t work with a basic query such as



    SELECT DISTINCT prod_number FROM mytable ORDER BY id DESC

    But is there a (simple) solution for this.

    Someone?

    Thx...

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    So, it sounds like you have multiple prod_number records (duplicates), but with different Autonumbers and you want to collapse them down to a single record.
    So, what is the logic for which Autonumber record you want to return/sort on? The highest one? The lowest one?

  3. #3
    hooty is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    4
    hi,
    the last record added should always come up first when displaying the table records.




    Using "SELECT DISTINCT (PRODUCT_CODE_LONG) FROM (TMP_ORDERS) ORDER BY PRODUCT_CODE_LONG DESC"

    Record 8 (Asc) or 10 (Desc) came up first. But i want record 12 should came up firts.

    Thx...

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Try using an Aggregate Query instead, like this:

    Code:
    SELECT
        PRODUCT_CODE_LONG,
        MAX(ID)
    FROM
        TMP_ORDERS
    GROUP BY
        PRODUCT_CODE_LONG
    ORDER BY
        2 DESC;

  5. #5
    hooty is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    4
    Thank You, Thank You,Thank You, Thank You,Thank You, Thank You,Thank You, Thank You and again Thank You!

    This will do the job perfectly....

  6. #6
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Glad to help!

    If you get the chance, you may want to look into Aggregate Queries a bit more. They can be built using the Query Builder (without having to know VBA).
    They are very useful when trying to group records by certain fields, which performing some calculation on that group (like Count, Sum, Avg, Min, Max, etc).

  7. #7
    hooty is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    4
    oh okay...
    I will definitely do that.
    thanks again

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Select Distinct Help
    By fionny in forum Queries
    Replies: 2
    Last Post: 06-28-2013, 08:24 AM
  2. Select distinct
    By Paul-NYS in forum Access
    Replies: 1
    Last Post: 07-09-2012, 09:03 AM
  3. How to use variable in SELECT DISTINCT
    By celtics11 in forum Access
    Replies: 1
    Last Post: 11-18-2011, 04:28 PM
  4. Replies: 8
    Last Post: 04-21-2011, 05:29 PM
  5. SELECT DISTINCT not working
    By johnmerlino in forum Queries
    Replies: 2
    Last Post: 10-25-2010, 06:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums