473,406 Members | 2,847 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,406 software developers and data experts.

problem with group by and maximum

Hi guys,
i have the following TABLE:

FIELD1 FIELD2 FIELD3
1 A 23
1 B 77 <<< i want to select this row
2 C 12
2 D 99 <<< and this one
2 E 17
3 ...

I need to select FIELD1 and FIELD2 where FIELD3 is a maximum of the
group grouped by FIELD1!

The following query works if i leave out FIELD2:
select FIELD1, max(FIELD3)
from TABLE
group by FIELD1

But i dont know how to get FIELD2 selected as well without getting a
SQL error.

Any ideas?

Thanks in advance.
Alex
Nov 12 '05 #1
2 1806
Alex Prentki wrote:
Hi guys,
i have the following TABLE:

FIELD1 FIELD2 FIELD3
1 A 23
1 B 77 <<< i want to select this row
2 C 12
2 D 99 <<< and this one
2 E 17
3 ...

I need to select FIELD1 and FIELD2 where FIELD3 is a maximum of the
group grouped by FIELD1!

The following query works if i leave out FIELD2:
select FIELD1, max(FIELD3)
from TABLE
group by FIELD1

But i dont know how to get FIELD2 selected as well without getting a
SQL error.

Any ideas?

Thanks in advance.
Alex

Try this:
select field1, field2, field3
from (select
field1,
field2,
field3,
row_number() over (partition by field1
order by field3 desc)
as rn
from table) AS X
where rn = 1;

The problem with group by is that there is no such thing as:
"Give me the row that produced the max() for this partition)"

Cheers
Serge
Nov 12 '05 #2
Alex Prentki wrote:
Hi guys,
i have the following TABLE:

FIELD1 FIELD2 FIELD3
1 A 23
1 B 77 <<< i want to select this row
2 C 12
2 D 99 <<< and this one
2 E 17
3 ...

I need to select FIELD1 and FIELD2 where FIELD3 is a maximum of the
group grouped by FIELD1!

The following query works if i leave out FIELD2:
select FIELD1, max(FIELD3)
from TABLE
group by FIELD1

But i dont know how to get FIELD2 selected as well without getting a
SQL error.


SELECT field1, field2, field3
FROM table
WHERE ( field1, field3 ) IN ( SELECT field1, MAX(field3)
FROM table
GROUP BY field1 )

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
11
by: Experienced but Undocumented | last post by:
Hi, I have a table with three columns: datetime, subject, & author. (There are more columns, but these are the only relavent ones.) Some rows will have identical subjects. For each subject,...
0
by: Guy Deprez | last post by:
Hi, i'm having a problem to create indexes. STEP 1 ----------- Connection is OK (you can find the string at the end of the message) Table ("Couleurs") creation is OK STEP 2. Index Creation
16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
5
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
2
by: crackerbox | last post by:
I have a table that I need to find the maximum date per Doc_No per Parent_Doccategory. I can get the maximum date per Doc_No but I can't get the information for the next level. The following script...
11
by: Mikael Arhelger | last post by:
Hello, This has been posted a few times but still I could not find a way to connect to our database. We run DB2 Express on WIN2K server with XP clients. I can ping inside network and to the...
2
by: paulquinlan100 | last post by:
Hi Hopefully someone can help with the following. The SQL statement below works correctly, selecting the maximum for each store (there will be several records for each store in the...
2
by: swethak | last post by:
hi , i write the code in .htm file. It is in cgi-bin/searches/one.htm.In that i write a form submitting and validations.But validations are not worked in that .htm file. I used the same code in my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.