I have three tables.
fundraisers
fundraiserId fundDescription fundName
14 testingfund test
15 analysis testing
fundraisingusers
fundraisinguserId amount fundraiserId userId
1 1000 14 12
2 2000 14 13
3 5000 15 14
users
userId firstName
12 xyz
13 pqr
14 abc
I am trying to display all fundraisers from fundraisers table with sum of amount from fundraisingusers table and user details from user table.I am writing query something like this.
SELECT f.fundraiserId as fundraiserId,
f.fundDescription as fundDescription,
f.fundName as fundName,
sum(fu.amount as amount),
fu.userId as userId FROM fundraisers as f
left outer join fundraisingusers as fu on f.fundraiserId =
fu.fundraiserId left outer join users as u on fu.userId =
u.userId";
Now i am able to get all the fundraisers with fundnames,fundDescriptions.But in amount i am not able to get the sum.
For example in fundraisingusers table i have two rows with same fundraiserId.Therefore in query as i am giving condition ON f.fundraiserId =fu.fundraiserId it is displaying fundraiser with fundraiserId 14 twice in the output with amounts showing empty.Can anyone tell how can i return unique rows from fundraising table if it has same fundraiserId more than once and also return sum of the amounts from fundraisingusers table with same fundraiserId.
My expected output is something like this.
fundraiserId fundDescription fundName amount userId
14 testingfund test 3000
15 analysis testing 5000
I want to display all fundraisers.If same fundraiserId is there then i want to display it only once with amount field added.In userId column if more than one users donate for same fundraiser then same fundraiser should display with sum of amounts.
group by.amountbroken down byfundraiserId, how would theuserIdfit into that? If you list all the individual users thesumof the amount would not be for the fundraiser but only for the specific user for a given fundraiser.user join not mandatoryis confusing as both are different ways of breaking down the data. What is it you want? The total amount per fundraiser or the total amount per user in each fundraiser?