473,327 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

problem with Select query

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
4 2765
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Juho Saarikko | last post by:
I made a Python script which takes Usenet message bodies from a database, decodes uuencoded contents and inserts them as Large Object into a PostGreSQL database. However, it appears that the to...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
1
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
3
by: StBond | last post by:
Hi everyone, I am new to Access and Visual Basic so things my be getting across a bit cloudy. I only started using VB for one week. I am having a little problem with the database that I am...
20
by: Development - multi.art.studio | last post by:
Hello everyone, i just upgraded my old postgres-database from version 7.1 to 7.4.2. i dumped out my 7.1 database (with pg_dump from 7.1) as an sql-file with copy-commands and to one file using...
0
by: mjsterz | last post by:
I've been working with VB .NET for less than a year and this is the first time I've posted on one of these groups, so let me apologize beforehand if I'm being unclear, not posting my issue...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
6
by: jej1216 | last post by:
I am trying to put together a PHP search page in which the user can select none, one, two, or three fields to search, and then the results php will build the SQL with dynamic where caluses to reflect...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
10
by: amitabh.mehra | last post by:
Hi I havent used MQT before. Read the online tips and tutorials but none seems to give any hint for my problem. I have a base table (base_table) as: st varchar(25) default...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.