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.
Author |
Topic |
natsucow
Starting Member
2 Posts |
Posted - 2013-07-07 : 14:31:06
|
I have two database tables for a leaderboard that I need to join together. The tables are leadbrdt17 and leadbrdt18. Each table represents one tour of data.Structure is:Table - leadbrdt17leadbdID - primarytour_day player_ID - links to foreign players tablebar_ID - links to foreign bars tablevisitor_pointsring_pointsftp_pointstotal_pointsStructure for leadbrdt18 is the exact same.There is also another table that I will need to join. The players table.Structure for players is:playerID - primaryplayer_name Ok, so I need a query to find the sum of the 'ring points' from both the 'leadbrdt17' and 'leadbrdt18' tables for each player on the players table. Essentially I need a list like this:PLAYER RING POINTSjim 750bob 500wendy 100I must have tried 30 different queries and looked at as many forum examples.I was thinking a union all with the two leaderboard tables along with a join on the players table... i appreciate any help/ |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-07-08 : 00:14:46
|
[code]SELECT Player_name, Sum(ring_Points)FROM (SELECT player_id, ring_points FROM leadbrdt17 union all SELECT player_id, ring_points FROM leadbrdt17 ) TJOIN Players p ON T.Player_id = p.Player_id[/code]GROUP BY Player_name--Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-08 : 01:39:32
|
it should be below provided you need to show all players regardless of whether they've points or notSELECT p.Player_name, COALESCE(Sum(T.ring_Points),0) AS TotalPointsFROM Players pLEFT JOIN(SELECT player_id, ring_points FROM leadbrdt17 union all SELECT player_id, ring_points FROM leadbrdt18 ) TON T.Player_id = p.Player_idGROUP BY p.Player_id ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
natsucow
Starting Member
2 Posts |
Posted - 2013-07-08 : 02:36:47
|
Thank both of you for the help. I used bandi's query and it did exactly what I want it to do. Well a couple of changes, but, I got the point.I don't need to show players without points but I appreciate your efforts visakh16. I have learned a lot from this.This is what I have now.SELECT player_name, Sum(ring_points)FROM (SELECT player_id, ring_points FROM leadbrdt17 union all SELECT player_id, ring_points FROM leadbrdt18 ) TJOIN players p ON T.player_id = p.playerIDGROUP BY Player_nameORDER BY Sum(ring_points) DESC |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-08 : 02:40:12
|
quote: Originally posted by natsucow Thank both of you for the help. I used bandi's query and it did exactly what I want it to do. Well a couple of changes, but, I got the point.I don't need to show players without points but I appreciate your efforts visakh16. I have learned a lot from this.This is what I have now.SELECT player_name, Sum(ring_points)FROM (SELECT player_id, ring_points FROM leadbrdt17 union all SELECT player_id, ring_points FROM leadbrdt18 ) TJOIN players p ON T.player_id = p.playerIDGROUP BY Player_nameORDER BY Sum(ring_points) DESC
Ok ..No problem you're welcomeGlad that I could help you in in understanding join basics------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|