Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two virtual table
table1 fields:B,sum(A)
table2 fields:B,sum(K)


Table1 data:
B sum(A)
1  20
2  30
3  40


Table2 data:
B sum(k)
5  20
6  70
3  20


Join results needed:B,sum(A),sum(K)

Bsum(A)sum(K)
120
230
34020
520
670
Posted
Updated 25-May-12 10:18am
v3

SQL
SELECT
    CASE WHEN T1.B IS NULL THEN T2.B ELSE T1.B END AS B,
    A,
    K
FROM
    Table_1 AS T1 FULL JOIN Table_2 AS T2
    ON T1.B = t2.B

I would suggest you read JOIN [^]fundamentals.
 
Share this answer
 
Comments
rajin kp 25-May-12 6:17am    
thanks Manas Bhardwaj
Maciej Los 25-May-12 6:22am    
Good work, Manas. +5!
I see that you are using aliases ;)
Manas Bhardwaj 25-May-12 6:43am    
Thanks!
Yeah, Always open to good ideas :)
Actually wat problem u r getting ?
 
Share this answer
 
Comments
rajin kp 25-May-12 5:59am    
If i use inner join matching rows selected
i need result as
B sum(A) sum(K)
1 20
2 30
3 40 20
5 20
6 70
Maciej Los 25-May-12 6:23am    
This is not an anser! Please, delete it. If you have any question or comment, use "Have a Question or Comment" button.
I think you need to use 'Union' or 'Union all' for this result
 
Share this answer
 
use Full Outer Join so that u can get match and non-match rows

SQL
select A.Sum(A),b.Sum(K) from Table1 As A full outer join Table as B on A.B=B.B
 
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