Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-09-29
2
Medium Priority
?
2,065 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 2000 total points
ID: 22599974
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
ID: 22603266
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

636 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