Click here to Skip to main content
15,905,776 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,


I have two tables. table1 and table2 as follows

Table1
id date amount

1 10/01/2011 100
1 16/01/2011 100
1 17/01/2011 100
2 01/01/2011 200
2 15/01/2011 200
2 18/01/2011 200
2 20/01/2011 200
3 10/01/2011 500


Table2
id name address

1 harry sydney
2 barry perth
3 cherry london

I want id, name ,address ,sum(amount) of the above three persons as follows


1-harry - sydney - 300
2-barry - perth - 800
3-cherry - london - 500


Thanks
Posted
Updated 1-Sep-11 3:37am
v2
Comments
Abhinav S 1-Sep-11 10:28am    
Please don't delete your question and repost.
That is a bad thing to do on a public forum.

Try as below Query.

SQL
select Table1.id, Table2.name, Table2.address, sum(Table1.amount) 
from Table2 inner join Table1 on Table2.id=Table1.id
group by Table1.id, Table2.name, Table2.address;
 
Share this answer
 
Comments
#realJSOP 1-Sep-11 9:37am    
5 - I failed to notice that the score was in table1. :)
RaisKazi 1-Sep-11 9:37am    
:)
Abhinav S 1-Sep-11 10:27am    
I had already posted a similar answer (missed the group by).
The OP deleted the question and reposted. Strange.

RaisKazi 1-Sep-11 10:35am    
Was not aware. I came across this problem twice today.
Try this:

SQL
select id, name, address, sum(amount) from table2 group by id, name, address
 
Share this answer
 
v2
Comments
RaisKazi 1-Sep-11 9:36am    
This Query will not work John. "amount" Column is in Table1, also inner join is require between two tables.
#realJSOP 1-Sep-11 9:38am    
Yep, already acknowledged.
RaisKazi 1-Sep-11 9:39am    
Comment was added before reading your comment:)

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