Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello there,


here is i'm obstructed in an sql query...
i've 1 table 'agents_details' like this..

Agent_Id,Agent_Supervisor_Id,Agent_Type

the Agent_Supervisor_Id may contain '0' or 1 value from Agent_Id

I'm having 1 Agent_Id as input and need to find all the Agent_Supervisor_Id in chain..


How can I do that in a query?

here is the details of agents_details table...

+--------+------------+----------------------
|Agent_Id| Agent_Type | Agent_Supervisor_Id |
+--------+------------+----------------------
|   1    | ME         |   0                |   
|   2    | AM         |   0                |    
|   3    | ME         |   2                |     
|   4    | ME         |   0                |    
|   5    | STM        |   7                |    
|   6    | ME         |   5                |   
|   7    | AM         |   0                |    
|   8    | ME         |   7                |    
|   9    | STM        |   5                |  




Thanx for Help...
Posted
Updated 8-Apr-11 10:20am
v2

As far as I know MySql isn't capable of handling recursive queries. So one possibility is to create a function (recursive) where you iterate through all the parent records and return for example a string containing all occurences.

Another option could be that if you know the maximum amount of levels you can 'hard-code' it to your statement using self-joins. For example
SELECT ...
FROM agents_details a1
     INNER JOIN agents_details a2 ON a1.Agent_Supervisor_Id = a2.AgentId
     INNER JOIN agents_details a3 ON a2.Agent_Supervisor_Id = a3.AgentId ...


[Addition]
For three levels
SQL
SELECT ...
FROM agents_details a1
     INNER JOIN agents_details a2 ON a1.Agent_Supervisor_Id = a2.AgentId
     INNER JOIN agents_details a3 ON a2.Agent_Supervisor_Id = a3.AgentId
     INNER JOIN agents_details a4 ON a3.Agent_Supervisor_Id = a4.AgentId
 
Share this answer
 
v2
Comments
DEB4u 9-Apr-11 1:36am    
Here i'm having 3 levels of iteration...How can i do that?
Wendelius 9-Apr-11 3:54am    
Answer updated
DEB4u 9-Apr-11 5:39am    
It's showing Ambiguous column 'AgentId'....

Can i writeby using 'connect by prior' like this..
"select Agent_Id from agents_details connect by prior Agent_Supervisor_Id=Agent_Id and Agent_Supervisor_Id>0 where Agent_Id=@aid1"

but i's showing error..
Wendelius 9-Apr-11 5:57am    
Actually I don't know if MySql supports connect by prior syntax. The error message means that you have the same column name in the query several times and you haven't used aliases. Try adding the aliases. Something like:
SELECT a1.AgentId, ...
Create a stored procedure,which takes a parameter agent_id, use cursors to iterate through each record, use the below query select Agent_Supervisor_Id from agent_details where agent_id=parameter, if above collected supervisor id is not equal to zero, iterate through the cursor,
SQL
select @SuperVisorID= Agent_Supervisor_Id  from agent_details where agent_id=parameter
WHILE @@SuperVisorID>0
BEGIN
    //set the above super visor id to a string, prepare a comma separated list of supervisors
   select @SuperVisorID= Agent_Supervisor_Id  from agent_details where agent_id=@@SuperVisorID

END

when you execute the above procedure, given 3 it should return 3,2

then execute some thing like select * from agent_details where agenit_id in (select @aboveProcName)
 
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