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 a.id = b.id and a.timestamp = b.timestamp
select top 1 from studenttable where id = @id group by ids order by timestamp desc
References :
SELECT TOP 1 * FROM studenttable WHERE id=(SELECT DISTINCT ids FROM studenttable) ORDER BY timestamp DESC
I believe that is correct, but I can be wrong….lol
It has been awhile since I played with database.
References :
http://www.simple-web-consulting.com
Give this a try:
SELECT a.* FROM studenttable a,
(SELECT id, MAX(timestamp) FROM studenttable GROUP BY id) b
WHERE a.id = b.id and a.timestamp = b.timestamp
References :