Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have two tables. Table1 contains Cus_id(primary key) and Cus_Name and it contains some record like this
C#
Cus_id  Cus_Name
---------------
101     xxxx
102     ABC
103     XXXX

And Table2 contains Regis_id,Cus_id(foreign Key),and sale_amount & it contains records like this
C#
Regis_id  Cus_id   sale_amount
------------------------------ ---
1          101      23000

I need out put like as follow as
C#
Cus_id  Sales_Amount
--------------------
101      23000
102
103


What is the query to get out put like this ? can anybody help me pls..
Posted
Updated 3-Nov-14 23:30pm
v2
Comments
Tomas Takac 4-Nov-14 5:30am    
What did you try? This is elementary.

use Left Join


Learn Before Use it :
http://www.dotnet-tricks.com/Tutorial/sqlserver/W1aI140312-Different-Types-of-SQL-Joins.html[^]

SQL
select a.cus_id,b. Sales_Amount from Table1 as a Left  join  table2 on a.Cus_id=b.Cus_id 
 
Share this answer
 
v2
Comments
Manas Bhardwaj 4-Nov-14 10:28am    
+5!
King Fisher 4-Nov-14 23:13pm    
Thank you :)
Try this:
SQL
SELECT t1.Cus_id, t1.Cus_Name, SUM(t2.Amount) AS Amount
FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t1.Cus_id = t2.Cus_id
GROUP BY t1.Cus_id
 
Share this answer
 
Comments
King Fisher 4-Nov-14 5:42am    
i agree :) my5+
Maciej Los 4-Nov-14 5:46am    
Thank you, King ;)
Manas Bhardwaj 4-Nov-14 10:27am    
my 5
Maciej Los 4-Nov-14 10:35am    
Thank you ;)
Try this code.
SQL
select a.Cus_id,b.sale_amount from table2  b right join table1  a on b.Cus_id=a.Cus_id
 
Share this answer
 
v2

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