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