Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I am having 2 tables as follows,

Table 1
HTML
ID      Name    PayAmount
1       A        1000
2       B        2000
3       C        1000


Table 2
HTML
ID          PaidAmount
 1           600
 2           2000
 4           1000


Here i need output as follows
HTML
ID         Name   BalanceAmt
 1           A     400
 3           c     1000
Posted
Updated 18-Jan-13 23:42pm
v2

Supposing there is only one row for an id in Table2 as in your example:
SQL
select
 t1.ID, t1.Name, t1.PayAmount-coalesce(t2.PaidAmount,0) as BalanceAmt
 from Table1 t1
 left join Table2 t2 on t1.ID = t2.ID
 where
 t1.PayAmount-coalesce(t2.PaidAmount,0) > 0
 order by t1.ID
 
Share this answer
 
Comments
itsureshuk 19-Jan-13 6:25am    
Thnks
Here is query

SQL
SELECT T1.Id AS Id,
       T1.Name AS Name,
       (T1.PayAmount - T2.PaidAmount) AS BalanceAmt
FROM Table1 AS T1 LEFT OUTER JOIN Table2 AS T2 ON T1.Id=T2.Id
WHERE t1.PayAmount-coalesce(t2.PaidAmount,0) > 0


Hope this helps if yes then accept and vote the answer if not then revert back with your queries\
--Rahul Dhoble
 
Share this answer
 
v3
Comments
Zoltán Zörgő 19-Jan-13 5:56am    
Two mistakes:
- inner join will loose id=3
- your where caluse will select rows where the balance is negative, thus there was more payed as wanted
RDBurmon 19-Jan-13 5:59am    
Thanks
itsureshuk 19-Jan-13 6:10am    
Thnks
itsureshuk 19-Jan-13 6:37am    
rahul ,left outer join
RDBurmon 19-Jan-13 6:39am    
ahh ok

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