Select the latest record for each area in a table

  • I have 2 tables.

    Table 1 (VirtualDay) (5pm->4:59pm)

    DayID, DateStart

    Table 2 (RegisterLog)

    DayID, StoreID, RegisterCount

    in RegisterLog, I store the number of registers in a specific store. BUT I check to see if the number of registers has changed from the previous entry. I don't need to store a record if nothing has changed.

    None of this is a problem.

    What I want to get is a query which will return the latest record for ALL the stores but I only want 1 row returned for each store with the DAYID (when the change occured), the StoreID(of course), and the RegisterCount.

    Is this something I can do in a stored proc, or should I just get the lot, and handle it in my application??

    Help really appreciated

  • This was removed by the editor as SPAM

  • try this:

    SELECT dt1.LastDayID, rl.StoreID, rl.RegisterCount

    FROM RegisterLog rl

    INNER JOIN

    (

    SELECT RegisterLog.StoreID, MAX(DayID) AS DayID

    FROM RegisterLog

    GROUP BY RegisterLog.StoreID

    ) AS dt1

    ON rl.DayID = dt1.DayID

    AND rl.StoreID = dt1.StoreID

    ORDER BY rl.StoreID

  • Thanks so much.

    I took this code, and made it work perfectly. Thanks for taking the time to reply.

Viewing 4 posts - 1 through 3 (of 3 total)

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