Forum Discussion

JasR09's avatar
JasR09
Copper Contributor
Feb 21, 2021
Solved

SQL 2012 query help on custom audit logs

I need some help creating a query on an audit table to summarise certain events. In the sample table below, I need to show the latest EntryURL for each user along with the Question1 answered by each ...
  • olafhelper's avatar
    olafhelper
    Feb 23, 2021

    That's just a small modification of the sub-query: Compare on Date and the ID must be small the the one of "Question1" entry

     

    SELECT main.UserID, main.Date, main.Attribute, main.Value,
           (SELECT TOP 1 sub.Value
            FROM yourTable AS sub
            WHERE sub.UserID = main.UserID
                  AND sub.Date = main.Date
                  AND sub.Attribute = 'EntryURL'
                  AND sub.ID < main.ID
            ORDER BY sub.ID DESC) AS LastEntryZRL
    FROM yourTable AS main
    WHERE main.Attribute = 'Question1'

Resources