A little trick Madhivanan taught me...SELECT T1.testid, T1.name, T1.score, T1.sectionFROM (SELECT COUNT(testid) - 1 AS Count, testid, name, score, section FROM test GROUP BY testid, name, score, section) AS T1 INNER JOIN master.dbo.spt_values AS T2 ON T1.Count > T2.numberWHERE (t2.name IS NULL)
Using the master.dbo.spt_values you are sure to get a complete number list that starts with 0.If on the other hand, your testid will always increment by 1, then you could just as well join your test table to itself like...SELECT T1.testid, T1.name, T1.score, T1.sectionFROM (SELECT COUNT(testid) AS Count, testid, name, score, section FROM test GROUP BY testid, name, score, section) AS T1 INNER JOIN (SELECT testid FROM test AS test_1 GROUP BY testid) AS T2 ON T1.Count > T2.testid
This returns the matches in the exact order you requeted.Cheers.