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????
 
Microsoft SQL Server 2005

Avatar of undefined
Last Comment
Mark Wills

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Thomasian

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
ursangel

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Your help has saved me hundreds of hours of internet surfing.
fblack61