Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using SUM on Multiple Tables

Author  Topic 

mfindlay
Starting Member

5 Posts

Posted - 2008-04-08 : 21:27:11
Hello,

I have 2 tables I am using as test, and for the life of me I can't figure this out.

Table: stats
Columns: ID, Name, Goals, Assists, Points
Example: 1, George, 1, 1, 2

Table: Players
Columns: ID, Name, Pick1, Pick2, Pick3
Example: 1, Bob, 1, 4, 7

I want to find the SUM value of stat.Points, where Players.Pick1 = stats.ID for all picks.

Then Grouping them by Players.name

Am I creating the database schema wrong for this?

singularity
Posting Yak Master

153 Posts

Posted - 2008-04-08 : 22:13:32
select b.name, sum(a.points)
from stats a
join players b on a.id = b.pick1
group by b.name
Go to Top of Page

mfindlay
Starting Member

5 Posts

Posted - 2008-04-09 : 14:06:01
Thank you so much! I added:

join players b on a.id = b.pick1 or a.id = b.pick2 or a.id = b.pick3

etc... for all the picks. Much appreciated!
Go to Top of Page
   

- Advertisement -