Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Frnds,
I have 2 tables as follows.
  Table1
EmpId    EmpName

 1         A
 2         B
 3         C
   Table2
PayId   PayAmount

 1     1000
 2     2000

 Table3
PayId     EmpId     PaidAmount  PaidDate

  1         1          300       21/5/2010
  1         1          300       11/6/2012
  2         1         1000       12/10/2012
  1         2         1000       28/4/2012
  2         2         2000       01/06/2012

when i pass payId(for eg:PayId=1) to Table3...
I need output as follows...
EmpId    PayId PayAmount PaidAmount 
  1         1      1000     600
  2         1      2000    1000
  3         1      1000       0

Thanks ASAP
Posted
Updated 20-Jan-13 1:07am
v3

1 solution

Try this
SQL
SELECT T3.EmpId,T3.PayId,Table2.PayAmount,T3.PaidAmount FROM
(SELECT EmpId,PayId,SUM(PaidAmount) AS PaidAmount
FROM Table3
GROUP BY EmpId, PayID) AS T3
INNER JOIN Table2
ON Table2.PayId=T3.PayId 

Output
EmpId       PayId       PayAmount   PaidAmount
----------- ----------- ----------- -----------
1           1           1000        600
2           1           1000        1000
1           2           2000        1000
2           2           2000        2000


Check your sample output, it seems not correct.
------------------------------------------------------------------------------------

Well, With regarding your comments, I assumed each employee join with each PayID. Therefore we have to use CROSS JOIN for that

SQL
SELECT Table1.EmpId,Table1.EmpName,Table2.PayId,Table2.PayAmount,ISNULL(PaidAmount,0)AS PaidAmount
FROM Table1
CROSS JOIN Table2
LEFT OUTER JOIN 
(SELECT EmpId,PayId,SUM(PaidAmount) AS PaidAmount
FROM Table3
GROUP BY EmpId, PayID) AS T3
ON T3.PayId =Table2.PayId AND T3.EmpId = Table1.EmpId
WHERE Table2.PayId=1


you will get hte following output

EmpId       EmpName PayId       PayAmount   PaidAmount
----------- ------- ----------- ----------- -----------
1           A       1           1000        600
2           B       1           1000        1000
3           C       1           1000        0


Please check your sample output, pay amount of EmpId 2 is not correct. It should be 1000 if you pass PayID=1
 
Share this answer
 
v2
Comments
itsureshuk 20-Jan-13 2:37am    
if employee id 3 is present,but still not paid any amount then how to show 3rd table one more row as : empid=3 payid=1 PayAmount=1000 PaidAmount=0
Tharaka MTR 20-Jan-13 3:03am    
My question is, how do you get the payamount and payid of the employee3?
How do you join them?
According to your question, we can only able to link 3 table using Table3.
how do you able to like the record without table3
itsureshuk 20-Jan-13 7:08am    
have 2 compare all tables,I need unpaid person 'C' also
Tharaka MTR 20-Jan-13 7:37am    
yes, I know that. but could you please tell me if there is no record for person "C" in table 3, then how do you join these table?

with your current design you can't join Table1 and Table 2. It is based on Table3. without any records for the C in table 3, how do we join that?

Check your table data, and how we decide Employee 3 belongs to PayID 1? is it default condition?

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