Results 1 to 6 of 6

Thread: [RESOLVED] Sum Query Help

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Sum Query Help

    SQL Code:
    1. SELECT Table1.UserName, SUM(Table3.Money)
    2. FROM Table1
    3. INNER JOIN Table2 ON Table1.ID = Table2.UserID
    4. INNER JOIN Table3 ON Table2.FriendName = Table3.FriendName
    5. GROUP BY Table1.UserName
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Resolved Re: Sum Query Help

    Simply awesome jmcilhinney...thanks

  4. #4

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RE-OPENED] Sum Query Help

    Quote Originally Posted by lintz View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    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
  •  



Click Here to Expand Forum to Full Width