|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
unique id'sappreciated. The table structure is : Create table temp (id int, dt datetime) insert into temp SELECT 424, '2005-04-04 16:29:00' UNION SELECT 408, '2005-02-25 11:12:00' UNION SELECT 167, '2005-02-23 16:51:00' UNION SELECT 426, '2005-02-23 11:22:00' UNION SELECT 411, '2005-02-22 16:59:00' UNION SELECT 416, '2005-02-22 09:54:00' UNION SELECT 426, '2005-02-14 11:13:00' UNION SELECT 426, '2005-02-04 10:07:00' UNION SELECT 426, '2005-02-04 09:43:00' UNION SELECT 426, '2005-02-04 09:36:00' UNION SELECT 426, '2005-02-03 17:31:00' UNION SELECT 167, '2005-01-25 12:37:00' UNION SELECT 167, '2005-01-25 12:27:00' UNION SELECT 425, '2005-01-25 10:19:00' UNION SELECT 424, '2005-01-17 16:15:00' UNION SELECT 167, '2005-01-11 10:45:00' UNION SELECT 207, '2005-01-06 17:12:00' UNION SELECT 417, '2005-01-06 14:12:00' UNION SELECT 421, '2005-01-05 16:38:00' UNION SELECT 396, '2004-11-14 10:31:00' drop table temp >> I need to find the 10 most recent unique ids. SELECT DISTINCT idFROM temp t1 WHERE ( SELECT COUNT( DISTINCT id ) FROM temp t2 WHERE t2.dt <= t1.dt ) <= 10 ; You can re-write this with a self-join and HAVING clause or use TOP in a correlated subquery. -- Anith As a patch-work, you can add the following as a subquery in your SELECT
clause : ( SELECT MAX( t2.dt ) FROM temp t2 WHERE t2.id = t1.id ) Otherwise you can re-write the query differently using a JOIN. -- Anith |
|||||||||||||||||||||||