select key, val from t1 where val =
(select min(sum_val) from
(SELECT KEY, SUM(VAL) sum_val FROM T1 GROUP BY KEY))
Sorry! Try the above query
Main Topics
Browse All TopicsI can't seem to find a way to fashion a query to do something that feels pretty simple. I have created some test data and queries in the attached code that demonstrate the problem. In this simple case, I have a table with a KEY column, and a VAL column. I want to build a query that returns a single row showing the KEY that has the smallest SUM of it's VAL's.
When I took my first shot at it I got the ORA-00937 error. So I tried to work around that with two nested queries, but that returns more than one row. My desired result would be a single row showing the a KEY of "A" and having a total VAL of 30.
~bp
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
select key,sumval from
(select key,sumval,ROW_NUMBER () over (order by sumval) sortrank from
(select key,sum(val) sumval from t1
group by key))
where sortrank = 1
Sorry! My brain does not work when Oracle is offline :-))
I think the above query should work. Am adding row number to the result (after sorting by sum of val field) and selecting the first record alone
Business Accounts
Answer for Membership
by: ravindran_eeePosted on 2009-09-07 at 08:01:03ID: 25275488
I think the below query will work
select key, val from t1 where
val = (select min(val) from t1)
You might have to add DISTINCT clause in sub query if there are going to be duplicate values for VAL (like A,10 and B,10)