Solved

Select Max Value in Column 1 based upon Distinct Value in Column 2

Posted on 2008-09-29
2
2,052 Views
Last Modified: 2012-05-05
Experts

I am looking to finalise a SQL query which should present a subset of data from an initial query which is based upon an outer join on 2 tables.

The second query should return the maximum value in column 1 associated with each distinct value in column 2.

For example, if the first outer join returns:

Column 1   Column 2        Column 3    Column 4

123            1111               x                 y
234            1111               a                 b
456            2222               c                 d
457            2222               c                 d
267            2223               a                 b

the second query should return:

Column 1   Column 2        Column 3    Column 4

234            1111               a                 b
457            2222               c                 d
267            2223               a                 b

I would like to perform this within a single SQL statement and avoid the use of temporary tables and nested cursors if at all possible.

Any pointers would be greatly appreciated!


 
0
Comment
Question by:campbell1972
2 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 total points
Comment Utility
select * from query1 a join (select max(column1) maxc1, column2 from query1) b on a.column1 = b.maxc1 and a.column2 = b.column2
0
 
LVL 14

Expert Comment

by:Binuth
Comment Utility
use GROUP BY too....
SELECT Query1.* 

FROM Table1 AS Query1

INNER JOIN (SELECT MAX(Column1) AS Col1,Column2 FROM Table1 GROUP BY Column2) Query2 

	ON Query1.Column1 = Query2.Col1	AND Query1.Column2 = Query2.Column2

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now