You are here: Re: Query question « MsSQL Server « IT news, forums, messages
Re: Query question

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация