Link to home
Start Free TrialLog in
Avatar of rafaelrgl
rafaelrgl

asked on

function sum on sql

hi, i want to do this with those tables:

table1:

id      username      name

2       bob23            bob tayl
3       testdf             lkkldfs ds
4       mary3            maryana


table2:

id      idusername     points

1      2                       30
2      2                       20
3      4                       60
4      3                       15
5      4                       10


so i want a table like this:


id    idusuario    username        name          points

1     4                mary3            maryana       60
2     2                bob23            bob tayl        50
3     3                testdf             lkkldfs ds      15

what will be the right syntax for this sql 2005 procedure????
 
ASKER CERTIFIED SOLUTION
Avatar of Thomasian
Thomasian
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark Wills
Hi, if you just want those records that exist in table 2 then you have to use an inner join to match your output example...

the ID in your output results is most ineresting - it reflects a new ID sequence, and is really not needed unless you plan to uniquely identify the new result set... Is this a requirement ?


SELECT t1.id as idusuario, t1.username, t1.name, SUM(t2.points) as points
FROM table1 t1
INNER JOIN table2 t2 ON t1.id=t2.idusername
GROUP BY t1.id , t1.username, t1.name
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial