Posted by Erland Sommarskog on 11/30/05 14:00
Yannick Turgeon (vendredi5h@gmail.com) writes:
> I often face this kind of query and I'm always wondering if there is a
> better way to do that. Here is the way I do this:
>
>
> ----------------------------------------
> SELECT data
> FROM #Test
> WHERE pid = (SELECT TOP 1 pid FROM #Test WHERE type = 'A' ORDER BY pid
> DESC)
> ----------------------------------------
>
>
> I suspect this is not the more comprehensive way to do this. Anybody do
> this differently?
Another variation:
SELECT a.data
FROM #Test a
JOIN (SELECT type, pid = MAX(pid)
FROM #Test
GROUP BY type) AS b ON a.type = b.type
AND a.pid = b.pid
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Navigation:
[Reply to this message]
|