q1: given a table : ID, Date, Status. Write a SQL query which receives @ID @Date and retrieve the status the person with that id on that date or the last status not later than that @date if that date doesn’t exist.
I have tried to write queries for above questions. I would appreciate you remarks:
SELECT TOP (1) status from MyTable AS T
WHERE T.Date <= @date && T.id = @id
ORDER BY T.Date
Thanks guys
You need to use
ANDinstead of&&, and order by date descending: