Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
See more:
Hi I have bellow tables:

Person Table:
id | name
1 | albert
2 | sam
3 | adel
...

transaction table:
tID | pID | price
1 | 1 | 1000
2 | 1 | 1200
3 | 3 | 780
...

I want a report like this:

name | sumOfPrice
albert | 2200
sam | 0
adel | 780

i try with "group by" on "transaction table" but this statement give me a report like below and "sam" has no record, what is solution?!

name | sumOfPrice
albert | 2200
adel | 780

thanks

[edit] SQL query added
SQL
SELECT Person.name, Sum(transaction.price) AS SumOfPrice
FROM Person RIGHT JOIN transaction ON Person.id = transaction.pID
GROUP BY Person.name
ORDER BY Person.name

[/edit]
Posted
Updated 16-Oct-13 6:08am
v4
Comments
phil.o 16-Oct-13 11:39am    
Solution to what? If there is no record for sam in the transaction table, then it is not a bug that the sum appears as 0.
You could exclude zero-sums with the following statement
WHERE SUM(price) <> 0
Edit: having the SQL request you are using could be useful, too.
maysamfth 16-Oct-13 11:43am    
thanks, this is my query: SELECT Person.name, Sum(transaction.price) AS SumOfPrice
FROM Person RIGHT JOIN transaction ON Person.id = transaction.pID
GROUP BY Person.name
ORDER BY Person.name
phil.o 16-Oct-13 11:48am    
Please do not post your query in a comment as it is hardly readable.
Better edit (improve) your question and qualify it with relevant informations.
maysamfth 16-Oct-13 11:53am    
i want that: all of person that saved in persons table whether has transaction or not, reported with query. if person have record in transaction table then sum of price be reproted, else if person have not record then zero be reported.
phil.o 16-Oct-13 11:55am    
Then see my solution ; a LEFT JOIN should do the trick.

You could try with inversing your join:
SQL
SELECT Person.name, Sum(transaction.price) AS SumOfPrice
FROM Person LEFT JOIN transaction ON Person.id = transaction.pID
GROUP BY Person.name
ORDER BY Person.name

and see if it fits your needs.
 
Share this answer
 
Comments
maysamfth 16-Oct-13 12:00pm    
thanks, but it dosent work. Your query delete all of records in person table that have no name.
phil.o 16-Oct-13 12:07pm    
A SELECT query cannot delete anything. Try with INNER JOIN instead.
maysamfth 16-Oct-13 12:10pm    
oh, no. in other wise it dont show all of records in person table that have no name!
phil.o 16-Oct-13 12:12pm    
If you want persons without name you should include Person.id in the query.
But I think that if you have persons without any name in your table, there is still a problem ; that should not happen.
maysamfth 16-Oct-13 12:19pm    
excuse me it work with LEFT JOIN! i forget remove a where statement! thanks
You can try:-

SQL
select Person.Name,sum(transaction.Price) as SumOfPrice from Person left outer join transaction 
on Person.ID=transaction.Pid group by Person.Name order by Person.Name


here for sam the SumOfPrice will be null
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900