tags:

views:

162

answers:

1

I need to select unique codes and their sum(value) values. This does not work:

select distinct t1.code, (
    select sum(t2.value) from table2 t2
    where t2.code = t1.code
)
from table1 t1

example table:

Code Value
ABC  1
ABC  2
BCD  12345

expected:

Code Value
ABC  3
BCD  12345

actual:

Code Value
ABC  (null)
BCD  (null)

Thanks in advance!

+2  A: 

A GROUP BY should suffice.

SELECT  Code, SUM(Value)
FROM    Table2
GROUP BY Code
Lieven
perfect, thank you. brainfart i suppose
David