Results 1 to 8 of 8

Thread: Trying to select most recent date

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Trying to select most recent date

    SQL Server 2008

    I have a parent table "tblParent" which has 2 child tables in a 1 to many relationship "tblChild1" and "tblChild2". Both child tables have a Date field "CreatedDate".

    I need to perform a join between the parent table and the 2 child tables and I need to get the "CreatedDate" which is most recent.

    I'm not sure how to do this as the 2 date fields reside in different tables.

    Any Ideas guys/gals ??

    Thanks In Advance

    Parksie

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Trying to select most recent date

    Try like this

    Select someKey,MAX(CreateDate) From (
    Select SomeKey Max(CreatedDate) From tblChild1 group by SomeKey
    Union
    Select SomeKey Max(CreatedDate) From tblChild2 group by SomeKey
    )
    Group By SomeKey
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Trying to select most recent date

    Looks nice but can I ask:

    Does the someKey refer to the foreign key in each of the child tables?

    Parksie

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Trying to select most recent date

    Yes... I assume that the PK of the Parent table is in each child table
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Trying to select most recent date

    Quote Originally Posted by venerable bede View Post
    SQL Server 2008

    I have a parent table "tblParent" which has 2 child tables in a 1 to many relationship "tblChild1" and "tblChild2". Both child tables have a Date field "CreatedDate".

    I need to perform a join between the parent table and the 2 child tables and I need to get the "CreatedDate" which is most recent.

    I'm not sure how to do this as the 2 date fields reside in different tables.

    Any Ideas guys/gals ??

    Thanks In Advance
    Do you want both rows - from tblChild1 and tblChild2 - but just the most recent from both?

    Please be more clear.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Trying to select most recent date

    Damn.
    I just realised that I also need to check a "deleted" field in each of the child tables for 0 and because its not part of the aggregate or group then I cant use it.Back to the drawing board.

    Parksie

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Trying to select most recent date

    Still sounds like just a sub-query with a where clause in the main where clause of a UNION'd select.

    I'm still in need of clarification.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Trying to select most recent date

    Its done.
    I basically have the following :
    Code:
    select userID,  MAX(d1) as MaxCreationDate,  MAX(d2) as MaxUpdatedDate from
    (
    -- Select blah blah
    ) as temp
    where temp.userID = @userId
    group by temp.userID
    What I need to do now is check to see which of the 2 dates is most recent and return that unless they are both null in which case return -1.

    Parksie

Posting Permissions

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



Click Here to Expand Forum to Full Width