I need help creating a query for the most recent records for unique ids in a table?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scott Barnette

    #1

    I need help creating a query for the most recent records for unique ids in a table?

    I have a table called project_notes.
    In this table users submit multiple notes on a daily basis for various projects.
    Fields: Id, ProjectId, Notes, CreatedOn

    How can i create a query for the Most recent Note for each ProjectId in the table?
  • gpl
    New Member
    • Jul 2007
    • 152

    #2
    Try something like this
    Code:
    Select pn.ProjectId, pn.Notes
    from project_notes pn
    inner join
    (
        select ProjectId, max(CreatedOn) as Created
        from project_notes
        group by ProjectId
    ) k
    on pn.ProjectId=k.ProjectId
    and pn.CreatedOn=k.Created
    If your database was normalised, then the notes would be in a separate table and you could select the top value for a project quite easily
    Last edited by gpl; Oct 6 '10, 06:19 PM. Reason: added normalisation note

    Comment

    Working...