Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. .
I have two tables it is like below.

Vendor Table

SQL
VendorId(int)        VendorName(varchar)
----------------------------
1                    Nirav
2                    Axay
3                    Jay
4                    Rajiv
5                    Urvish


and other table

TXN Table

SQL
VendorId(int) Amount(int) ToCompany(int) Is Paid(bit)
--------------------------------------------------
1	1000	500	FALSE
1	2850	852	FALSE
1	3508	963	TRUE
2	2587	254	TRUE
2	1234	212	FALSE
2	1000	321	FALSE
2	5000	654	TRUE



now i want output like this way

SQL
VendorId    	Not Paid
-------------------------------------------------------------------
1	total of all  ToCompany where isPaid=False and vendorid=1
2	total of all  ToCompany where isPaid=False and vendorid=2




what should i do for it?
Posted
Updated 9-Aug-13 2:39am
v5

1 solution

SQL
SELECT v.VendorName, SUM(COALESCE(t.Amount,0)) AS TotalAmount
FROM Vendor V
LEFT JOIN TXN t ON v.VendorId = t.VendorId AND t.isPaid = false
GROUP BY v.VendorName
 
Share this answer
 
Comments
Maciej Los 9-Aug-13 9:00am    
Looks perfect!

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