| | |
aggregate on multiple joins from one parent table
Please support our MS SQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 8
Reputation:
Solved Threads: 0
Hello all,
I'm having the following problem,
I have three tables, Person, Income, Outcome
Person has the primary key : ID
Income has two fields : PersonID [Foreign] and IncomeAmount
Outcome has two fields : PersonID [foreign] and OutcomeAmount
I need to make a query that gets the summation of IncomeAmount and the summation of OutcomeAmount for each personID
My Query which gets totally wrong data is:
Why does it gets wrong data?
what am I missing here ?
thanks
I'm having the following problem,
I have three tables, Person, Income, Outcome
Person has the primary key : ID
Income has two fields : PersonID [Foreign] and IncomeAmount
Outcome has two fields : PersonID [foreign] and OutcomeAmount
I need to make a query that gets the summation of IncomeAmount and the summation of OutcomeAmount for each personID
My Query which gets totally wrong data is:
SELECT Person.ID, Sum(Income.IncomeAmount) , Sum(Outcome.OutcomeAmount) From Person,Outcome,Income Where Person.ID = Income.PersonID AND Person.ID = Outcome.PersonID Group by Person.ID
Why does it gets wrong data?
what am I missing here ?
thanks
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
MS SQL Syntax (Toggle Plain Text)
SELECT p.ID, (SELECT SUM(i.IncomeAmount) FROM Income i WHERE i.PersonId = p.ID) AS INCOME_AMOUNT, (SELECT SUM(o.OutcomeAmount) FROM Outcome o WHERE o.PersonId = p.ID) AS OUTCOME_AMOUNT FROM Person p
Last edited by dickersonka; Mar 17th, 2009 at 4:36 pm.
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
![]() |
Other Threads in the MS SQL Forum
- Previous Thread: sql server remote access and user auithentication issue
- Next Thread: Report Server URL Not Vaild Error
Views: 698 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last age autogrowth business connectingtodatabaseinuse count cursor data database dateadd datediff datepart day" dbsize deadlock delete_trigger exploit getdate hack highperformancecomputing hpc hpcserver2008 ibm iis limit live loop maximum microsoft ms mssql multiple multithreading news number password permission position query reporting result security server services sets single source sql sql-injection sqlserver sqlserver2005 subtype supercomputing supertype tables uniqueid update view weekday






