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

3 Responses to “SQL programming: how to combine these two statements?”

  1. select top 1 from studenttable where id = @id group by ids order by timestamp desc
    References :

  2. 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

  3. TheMadProfessor on January 18th, 2010 at 3:04 am

    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 :

Leave a Reply