hi all geeks,
I have a problem regarding joins.
I have 2 tables,
Customer ( Customer_code, Agent1, Agent2, Agent3)
Agent( Agent_Code, Agent_name)
and data is like this:
Now I want to retrieve all customer_code with their corresponding agent names, like
1 X Y Z
2 Y Z P
3 X P Z
, any suggestions please,
thanks a lot.
You can use a query like this
Select Customer_code,
(select agent_name from Agent where agent_code=Agent1),
(select agent_name from Agent where agent_code=Agent2),
(select agent_name from Agent where agent_code=Agent3)
from Customer
||| thanks a lot Mr. Abdul
if you really need relations use this code:
createtable #Cust( customer_codeint,
Agent1int,
Agent2int,
Agent3int)
createtable #agents(AgentCodeint,
AgentNamevarchar(10))
insertinto #custSELECT 1,1,2,3
insertinto #custSELECT 2,2,3,4
insertinto #custSELECT 3,1,4,3
insertinto #agentsSELECT 1,'X'
insertinto #agentsSELECT 2,'Y'
insertinto #agentsSELECT 3,'Z'
insertinto #agentsSELECT 4,'P'
insertinto #agentsSELECT 5,'Q'
select*from #Cust
select*from #agents
Select customer_code,
a1.AgentName [Agent1Name],
a2.AgentName [Agent2Name],
a3.AgentName [Agent3name]from #cust
leftjoin #agents A1on Agent1= A1.AgentCode
leftjoin #agents A2on Agent2= A2.AgentCode
leftjoin #agents A3on Agent3= A3.AgentCode
droptable #cust
droptable #agents
No comments:
Post a Comment