|
-
Jan 21st, 2010, 10:19 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Sum Query Help
I've got 3 tables which I've outlined below. What I'm trying to display is the 'UserName' from table1 along with the sum of 'Money' from table 3 where 'FriendName' in table2 = 'FriendName' in table3.
So the end result would display. Any help would be appreciated.
Bill 150
Ben 50
Frank 0
Table1
ID UserName
1 Bill
2 Ben
3 Frank
Table2
UserID FriendName
1 Steve
1 John
2 Steve
3 Luke
Table3
FriendName Money
Steve 50
Mark 30
John 100
-
Jan 21st, 2010, 10:34 PM
#2
Re: Sum Query Help
SQL Code:
SELECT Table1.UserName, SUM(Table3.Money) FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.UserID INNER JOIN Table3 ON Table2.FriendName = Table3.FriendName GROUP BY Table1.UserName
-
Jan 21st, 2010, 10:50 PM
#3
Thread Starter
PowerPoster
-
Jan 27th, 2010, 11:49 PM
#4
Thread Starter
PowerPoster
Re: [RE-OPENED] Sum Query Help
The above query displays the following result:
Bill 150
Ben 50
Following on from the above query I am now trying to get the UserName from table1 where 'ID' in table1 = 'UserID' in table2 and 'FriendName' in table2 doesn't exist in table3.
The end result would be:
Frank
-
Jan 28th, 2010, 12:10 AM
#5
Re: [RE-OPENED] Sum Query Help
 Originally Posted by lintz
The above query displays the following result:
Bill 150
Ben 50
Following on from the above query I am now trying to get the UserName from table1 where 'ID' in table1 = 'UserID' in table2 and 'FriendName' in table2 doesn't exist in table3.
The end result would be:
Frank
New topic = new thread.
-
Jan 28th, 2010, 01:33 AM
#6
Re: [RESOLVED] Sum Query Help
Code:
Select Username
From Table1
Inner Join Table2
On Table1.ID = Table2.ID
Where Not Exists
(Select *
From Table3
Where Table2.FriendName = Table3.FriendName)
Mind you, if a user had 2 friends in table 2 and neither existed in table 3 you would see them twice. If you only want to see them once you probably want this:-
Code:
Select Username
From Table1
Where Not Exists
(Select *
From Table2
Inner Join Table3
On Table2.FriendName = Table3.FriendName
Where Table2.UserID = Table1.UserID)
although that would also include users with no friends at all.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|