Question

enter image description here

The above table has recursive relationship ie. the crfor column has reference to resourceid column.

So the number 1 in the crfor column means resourceid = 1 .How should I change my query to make crforname column (dynamic column) display the proper name Arjun instead of Ashok?

I used the query below to get the result above. You can ignore the other table call PCR in the query

select t1.*,t2.pcrname,t1.ResourceName AS crforName from 
Resource t1 left join Pcr t2  on t1.Pcrid=t2.pcrid   
where t1.contractid=1
Was it helpful?

Solution

Try adding a self join on the Resource table joining the ResourceId to the CrFor:

select t1.*,t2.pcrname,t3.ResourceName AS crforName from 
Resource t1 left join Pcr t2  on t1.Pcrid=t2.pcrid left join Resource t3 on t1.CrFor = t3.ResourceId
where t1.contractid=1 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top