You are here: Re: problem with Select query « MsSQL Server « IT news, forums, messages
Re: problem with Select query

Posted by Erland Sommarskog on 11/21/05 23:53

Sandy (a@a.com) writes:
> I have a table A (ID, time,...)
>
> first I want to select rows with max value of time. Then from these
> rows I want the row with max ID value. i am doing the following but its
> giving me the error mentioned below
>
> select max(ID) from (select * from A where time in ( select max(time)
> from A ) )
> ERROR: Incorrect syntax near ')'
>
> Where is the problem in my Query.

The problem is that the derived tables requires aliases:

select max(ID) from (select * from A where time in ( select max(time)
from A ) AS X ) AS Y

But maybe this query serves you better:

SELECT A.ID, A.time
FROM A
JOIN (SELECT time = MAX(time) FROM A) AS A1 ON A.time = A1.time



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

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