Home > Archive > Microsoft SQL Server forum > November 2005 > problem with Select query









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author problem with Select query
Sandy

2005-11-21, 7:23 am

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


Jens

2005-11-21, 7:23 am

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.

CK

2005-11-21, 9:23 am

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" <Jens@sqlserver2005.de> wrote in message
news:1132566796.857987.84390@g49g2000cwa.googlegroups.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.
>



Erland Sommarskog

2005-11-21, 8:24 pm

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/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx
--CELKO--

2005-11-21, 8:24 pm

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

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com