By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,045 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,045 IT Pros & Developers. It's quick & easy.

problem with Select query

P: n/a
Hi,

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. Is there any other way to do this??

any help is greatly appreciated.

Thanks
Nov 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
This could be a solution:
Select MAX(ID) FROM SomeTable
Where Time =
(
Select MAX(Time) From SomeTable
)

You were missing the aliases:
select max(ID) from
(
select * from A where time in
(
select max(time) from A
) SubQuery1
) SubQuery2
HTH, Jens Suessmeyer.

Nov 23 '05 #2

P: n/a
CK
Would this only give the the value of max ID as a result? I think Sandy
wants the complete row which has the max ID value.
"Jens" <Je**@sqlserver2005.de> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
This could be a solution:
Select MAX(ID) FROM SomeTable
Where Time =
(
Select MAX(Time) From SomeTable
)

You were missing the aliases:
select max(ID) from
(
select * from A where time in
(
select max(time) from A
) SubQuery1
) SubQuery2
HTH, Jens Suessmeyer.

Nov 23 '05 #3

P: n/a
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, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 23 '05 #4

P: n/a
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

Also data element names like "id" (of what entity?) and time (a
reserved word in SQL) are pretty useless. I am sure that 'A' is a
meaningful name in your industry ...

Nov 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.