I'm trying to implement recursion on query using Common Table Expression. However I'm not very much familiar with it. The scenario surrounds between parent and child cases where Parent when logged in can see his own as well as his all children cases and so on and so forth. But I'm unable to do this with using cte expression.
I have a user table and a relation table that contains both the primary and secondary user. The primary user is parent and secondary user is child.
Below is my query.
select *
from dbo.[Case] c
left join dbo.[User] u on c.AssignedTo = u.UserId
left join dbo.[User] uu on c.AssignedTo = uu.UserId
where uu.UserId in (select SecondaryUser from dbo.[Relations]
where PrimaryUser = 'f422233f-9c70-4052-81a5-e2525707df0b')
or u.UserId = 'f422233f-9c70-4052-81a5-e2525707df0b'
But the above query only returns one parent and one child case. And i want to have multiple parents and their multiple child case using common table expression.
Assuming some users as follows
Id Name Email
001 Salar [email protected]
002 Ather [email protected]
003 John [email protected]
And In RelationShip Table
Id PrimaryUser SecondaryUser
101 001 002
001 002 003
And their Assigned Cases
Id CaseTitle CaseDescription AssingedTo
101 Case 1 First case 001
102 Case 2 Second case 002
103 Case 3 Third case 003
So when i logged in to 001 id I should see all three cases, and when i logged in with 002 i should see last two cases.