SQL programming: how to combine these two statements?
How can I combine
select distinct ids from studenttable
……………then for each id…………………
select top 1 * from studenttable where id = @id order by timestamp desc
……into one T-SQL statement (IE without using loops/queues)?
BTW I meant cursors not queues
Give this a try:
SELECT a.* FROM studenttable a,
(SELECT id, MAX(timestamp) FROM studenttable GROUP BY id) b
WHERE [...]