Select query for latest records by date

  • Hello ,

    I'm trying to retrieve all the records for the most recent date.  I have used Max on the [entrydate] and this column is a datetime data type, however this only returns 1 record even though there are 12 records for the day.   is the datetime the issue - How do I retrieve all 12 rcords ?

    SELECT *
    FROM [Prod_ShiftSummary]
    WHERE entrydate= (
    SELECT MAX(EntryDate)
    FROM [Prod_ShiftSummary]
    where office = 'baytown'
    ) ;

    thank you in advance.

  • Presumably, the 12 records weren't all entered at exactly the same time on that date, but that is what you are checking for.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • SELECT *
    FROM [Prod_ShiftSummary]
    WHERE entrydate >= (
    SELECT CONVERT(date, MAX(EntryDate))
    FROM [Prod_ShiftSummary]
    where office = 'baytown'
    ) ;
  • Jonathan, OMG you are kidding me.....  I tried a bunch of things but not a >

    Thank you so much !

     

  • Now that Jonathan has given you a solution, this likely to perform better.

    WITH ShiftSummary AS
    (
    SELECT *, LAST_VALUE(ss.EntryDate) OVER(ORDER BY ss.EntryDate ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS LatestDatetime
    FROM Prod_ShiftSummary AS ss
    )
    SELECT *
    FROM ShiftSummary AS ss
    WHERE ss.EntryDate >= CAST(ss.LatestDatetime AS DATE);

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • This was removed by the editor as SPAM

  • This was removed by the editor as SPAM

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply