Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
My table has following fields and records

Billno Amount
1 200
1 200
2 300
2 300

i want to get sum of Amount in distinct(Billno),In this example My required answer is 500, but I didn't know how to do it,Please somebody help for this proble

Thanks in Advance
Posted
Comments
Amir Mahfoozi 10-Dec-11 11:56am    
Hi Devausha, What prevented you from accepting my solution as an answer ? :)

Here is one solution :

SQL
select SUM(amount) sum from(
select distinct billno, amount from bill) as distincted


Hope it helps.
 
Share this answer
 
SELECT SUM (DISTINCT Amount) FROM tablename
 
Share this answer
 
v3
Comments
Amir Mahfoozi 10-Dec-11 8:10am    
You query result is 3 :)
Pravinjas 14-Dec-11 6:23am    
Now it will give correct result...
Amir Mahfoozi 14-Dec-11 6:35am    
+5 yes it is.
Try this up!

SQL
SELECT SUM(Amount) FROM (SELECT DISTINCT BillNo FROM [TableName]


Regards,
Eduard
 
Share this answer
 
Comments
Amir Mahfoozi 10-Dec-11 12:36pm    
Your subquery doesn't have any Amount field to be summed up.
Select distict billno,sum(amount)as amount from table_name OR

select billno,sum(amount)as amount from table_name group by billno
 
Share this answer
 
Comments
Amir Mahfoozi 10-Dec-11 8:10am    
Please test your queries before sending as solution :)

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