473,717 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extract Rows with highest values in the columns, MAX doesn't work

From the table i want everything highlighted with a *

I wanted an SQl expression to look at values in Column 1 (ID), look at
the corresponding values in the second column (F1) and select the row
with the highest value, and then if there was more than one row for
that ID with the same value in F1, look at column 3 (F2) and select
the row with the highest value in this column.

SQL> select * from test;

ID F1 F2
---------- ---------- ----------
1 12 4
* 1 12 6
1 11 1
1 9 12
2 3 5
* 2 9 13
2 9 9
3 1 2
3 1 1
* 3 7 5

I try the following

SQL> select * from test t1
2 where f1 = (select max(f1) from test t2 where t2.id = t1.id)
3 ;

And get the following

ID F1 F2
---------- ---------- ----------
1 12 4
* 1 12 6
* 2 9 13
2 9 9
* 3 7 5

If I add another line with an AND statement after line 2 I either get
nothing or rubbish as the output.

Ideas?

Cheers
Jul 20 '05 #1
2 21186
If I understand correctly, this should get u going in the right direction:

select a.*
from test a
inner join (
select id
, f1
, max(f2) maxf2
from test
group by id
, f1
) b on b.id = a.id
and b.f1 = a.f1
and b.maxf2 = a.f2

Harry Helmich

sukh wrote:
From the table i want everything highlighted with a *

I wanted an SQl expression to look at values in Column 1 (ID), look at
the corresponding values in the second column (F1) and select the row
with the highest value, and then if there was more than one row for
that ID with the same value in F1, look at column 3 (F2) and select
the row with the highest value in this column.

SQL> select * from test;

ID F1 F2
---------- ---------- ----------
1 12 4
* 1 12 6
1 11 1
1 9 12
2 3 5
* 2 9 13
2 9 9
3 1 2
3 1 1
* 3 7 5

I try the following

SQL> select * from test t1
2 where f1 = (select max(f1) from test t2 where t2.id = t1.id)
3 ;

And get the following

ID F1 F2
---------- ---------- ----------
1 12 4
* 1 12 6
* 2 9 13
2 9 9
* 3 7 5

If I add another line with an AND statement after line 2 I either get
nothing or rubbish as the output.

Ideas?

Cheers

Jul 20 '05 #2
"sukh" <su**@jatt.co m> wrote in message news:e7******** *************** *@posting.googl e.com...
From the table i want everything highlighted with a *

I wanted an SQl expression to look at values in Column 1 (ID), look at
the corresponding values in the second column (F1) and select the row
with the highest value, and then if there was more than one row for
that ID with the same value in F1, look at column 3 (F2) and select
the row with the highest value in this column.

SQL> select * from test;

ID F1 F2
---------- ---------- ----------
1 12 4
* 1 12 6
1 11 1
1 9 12
2 3 5
* 2 9 13
2 9 9
3 1 2
3 1 1
* 3 7 5

I try the following

SQL> select * from test t1
2 where f1 = (select max(f1) from test t2 where t2.id = t1.id)
3 ;

And get the following

ID F1 F2
---------- ---------- ----------
1 12 4
* 1 12 6
* 2 9 13
2 9 9
* 3 7 5

If I add another line with an AND statement after line 2 I either get
nothing or rubbish as the output.

Ideas?

Cheers


CREATE TABLE Test
(
id INT NOT NULL,
f1 INT NOT NULL,
f2 INT NOT NULL,
PRIMARY KEY (id, f1, f2)
)

INSERT INTO Test (id, f1, f2)
VALUES (1, 12, 4)
INSERT INTO Test (id, f1, f2)
VALUES (1, 12, 6)
INSERT INTO Test (id, f1, f2)
VALUES (1, 11, 1)
INSERT INTO Test (id, f1, f2)
VALUES (1, 9, 12)
INSERT INTO Test (id, f1, f2)
VALUES (2, 3, 5)
INSERT INTO Test (id, f1, f2)
VALUES (2, 9, 13)
INSERT INTO Test (id, f1, f2)
VALUES (2, 9, 9)
INSERT INTO Test (id, f1, f2)
VALUES (3, 1, 2)
INSERT INTO Test (id, f1, f2)
VALUES (3, 1, 1)
INSERT INTO Test (id, f1, f2)
VALUES (3, 7, 5)

SELECT T1.*
FROM Test AS T1
LEFT OUTER JOIN
Test AS T2
ON T1.id = T2.id AND
(T2.f1 > T1.f1 OR (T2.f1 = T1.f1 AND T2.f2 > T1.f2))
WHERE T2.id IS NULL
ORDER BY T1.id

id f1 f2
1 12 6
2 9 13
3 7 5

Regards,
jag
Jul 20 '05 #3

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

Similar topics

9
1958
by: jason | last post by:
Access 2000 I need some help interogatting a table and extracting via ASP the final field in a row which has a value. In other words, I have a maximum of 10 fields but, at the user level he may he only enter values into the first four. I need to ALWAYS extract the final or last field that has a value. In the example below this would be PriceField5 which has a value of $162,000. The user stopped at this field and left the rest blank.
7
4451
by: robert | last post by:
running 8.1.7 server, 8.1.6 client. i *thought* inner join should not return nulls, but not only that, but i get way more rows than i'm expecting. assume: order table: order_number
7
6216
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to make it better soon. Unfortunately, there is never a non-crucial time in which we can do an upgrade, so we are stuck for now. Point 1: There are multiple tables: students, courses, cross-reference
12
39119
by: Rick DeBay | last post by:
I'm trying to create a layout table, where the spacing between rows varies. I've tried using setting margin-top and border-top for the rows I wan't spaced down from the one above, and I've also tried setting it on all of the cells of that row. This hasn't worked, so I was wondering if anyone knew how it could be done. Thanks, Rick DeBay
5
3096
by: Chad Richardson | last post by:
Is there a way in SQL Server 2000 to extract data from a table, such that the result is a text file in the format of "Insert Into..." statements, i.e. if the table has 5 rows, the result would be 5 lines of : insert into Table (, , .... VALUES a,b,c) insert into Table (, , .... VALUES d, e, f) insert into Table (, , .... VALUES g, h, i) insert into Table (, , .... VALUES j, k, l) insert into Table (, , .... VALUES m, n, o)
3
4879
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that the best method? Do you have a sample of how to do this?
3
2621
by: hazz | last post by:
The datareader below contains two rows of two columns but in the for loop, the values for only the first row are getting printed twice. How do I get to the values of the second row? Thanks. -hazz Dim dTable As DataTable Dim dsBlock As New DataSet Dim rowBlock As DataRow Dim myColumn As DataColumn Dim iCol As Integer = 0
68
6816
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
7
2740
by: figital | last post by:
Given an ID (column B), I need to find which IDs have identical data. That is, given '200', I want the desired result to be: 100 The idea is that the system sees that id=200 has 5 records with the indicated data in cols C and D. It should then find any other ids with the exact same data for those columns.
0
8823
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9202
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9048
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7981
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6648
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5970
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4739
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2545
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2120
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.