Click here to Skip to main content
15,877,591 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
i have two columns source_id and Qty in one table TRANS. 

Source_id   Qty
2002        100
2002        200 
2002        500
2002       -500

i want to get balance of this source_id which is 100+200=300 because (-500+500=0)
how can i write a query for this 
and can i show 100 and 200 saparate in two lines as balance


What I have tried:

i have two columns source_id and Qty in one table TRANS.

Source_id Qty
2002 100
2002 200
2002 500
2002 -500

i want to get balance of this source_id which is 100+200=300 because (-500+500=0)
how can i write a query for this
and can i show 100 and 200 saparate in two lines as balance
Posted
Updated 4-May-16 21:31pm
Comments
glen205 5-May-16 3:28am    
"What I have tried" ... is just your question repeated again.
What have you tried?

Google for information on the SQL aggregate functions: SUM and GROUP BY - this will get you started

What is the business case for "show 100 and 200 separate in two lines as balance"? If "balance" is the sum of all the transactions, why would you choose not to show two of them (the +500 and -500) - just because they net each other out? If I put 500 in my bank and then take it out again, I don't want that hidden on my statement because it's a net zero movement - what's your actual requirement here as the "100 and 200 separate" is not clear.

1 solution

You should use SQL GROUP BY Statement.

SQL
SELECT [Source_id], SUM([Qty]) FROM [YourTableName]
GROUP BY([Source_id])


"and can i show 100 and 200 saparate in two lines as balance"

I am unable to understand that.
 
Share this answer
 
v3

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