We help IT Professionals succeed at work.
Get Started

SQL Query for max of sum and look up

Last Modified: 2012-08-13
I have two tables (Table 1 & Table 2) as below

Table 1
ID      Code      V1      V2
--                  ------             ---                  ----
1      E-1      2      1
1      E-12      1      2
1      E-10      3      1
1      E-9      3      2
1      E-8      5      8
1      E-3      8      8
2      E-3      1      3
2      E-5      2      4

Table 2
Code      Desc
-----              -------
E-1      asd
E-2      dsa
E-3      ad
E-4      dd
E-5      aadas
E-6      asd
E-7      asdddd
E-8      asds
E-9      sds
E-10      qwqwe
E-11      da
E-12      cccc

For each ID in Table 1 , i need to get a Desc from table 2 which works as below

In Table 1, for each ID, I need to select a 'Code' which was maximum sum of V1 and V2

for that Code, I need the DESC.

How can i workout the query for this ?
Watch Question
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE