Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one table like below:

Id Pid Description DebitAmt CreditAmt
1 1 Transaction1 100.00 0.00
2 1 Transaction_1 0.00 100.00
3 2 Transaction2 200.00 0.00
4 2 Transaction_2 0.00 200.00





Here i want Result Like That:

Id Description Amount
1 Transaction1,Transaction_1 200.00

2 Transaction2,Transaction_2 400.00

Can somebody help me?
Thanks in advance
Posted
Updated 24-Feb-14 0:58am
v2

1 solution

You need GROUP BY to do this ... see example[^]

In this case
select Pid as Id, Description, Sum(DebitAmt) + Sum(CreditAmt) from oneTable GROUP BY Pid, Description ORDER BY Pid


Note this will give you the results mentioned in your post but not necessarily what you actually want ... you mentioned "Id" but "Pid" seems to be the foreign key, and it is somewhat unusual to sum credit and debit amounts together.

[EDIT - :facepalm] I didn't spot the underscore in the descriptions. PIVOT is probably more appropriate for this solution ... see this CodeProject tipSimple Way To Use Pivot In SQL Query[^]
 
Share this answer
 
v2
Comments
RahulRana723 24-Feb-14 6:56am    
Here I use PID for link two related transaction and i have two transactions one have CreditAmount and Another Have DebitAmount.i want to display these two rows into single row with Combine Description of each other and with (Sum of Credit and Debit)/2 of each other
CHill60 24-Feb-14 7:04am    
Apologies ... didn't see the underscore - Transaction1 and Transaction_1 - I'll update my 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