Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,537
    Thanks
    45
    Thanked 259 Times in 256 Posts

    Getting distinct values

    I am trying to figure out how I can pull the latest values of a distinct type from a table.

    For example, if I had a table of posts, with the columns postID (int), threadID (int), title (varchar, 50), and datePosted (datetime). How could I get the latest post from each threadID?

    I tried SELECT DISTINCT threadID, postID, title, datePosted FROM posts ORDER BY datePosted DESC, but it didn't work. The only other way I know to do it is to make a query table that reverses the order, then group by threadID, but that seems a bit convoluted. I tried doing a ORDER BY then GROUP BY, but it failed, and only worked when I grouped first, then ordered.

    Any other way to do it? Or is the dynamic query table the only way?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Try
    Code:
    CREATE or REAPLACE VIEW latestPostByThread AS
    SELECT max(postID) as maxId FROM posts GROUP BY threadID;
    
    SELECT threadID, postID, title, datePosted FROM posts
    INNER JOIN latestPostByThread on postID=maxID
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,979
    Thanks
    120
    Thanked 76 Times in 76 Posts
    for something similar I have:


    Code:
    Select ...
    (
    select max(date) as d , id
    FROM table
    group by id
    ) as t1 join (back to original table) table on t1.id = table.id ..

    result of that: http://www.agrozoo.net/jsp/gallery_last_comments.jsp
    If you click on bee & scroll down you will see 'test' is last comment of two, and only last is shown.

    Mybe having view inbetween is a bit faster, but not much I think, don't know ...
    Last edited by BubikolRamios; 09-23-2011 at 10:59 AM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,537
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Ah, ok, so we are on the same page then.

    I actually did already setup a view for it, which just extracts the last post for each thread, which I use to join, just like you did abduraooft.

    And your technique BubikolRamios is what I meant by "query table".

    I was curious if I had missed something in terms of using DISTINCT, or there was a third technique I didn't know. Either way, thanks guys!

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    I was curious if I had missed something in terms of using DISTINCT
    Yes, the Distinct keyword applies to all the columns in the Select clause and hence it generally works well when there's less number of columns. In your case, all the rows will be Distinct.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,537
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Ah, so doesn't work for what I'm trying to do. Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •