Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
nimda
Starting Member
4 Posts |
Posted - 2011-12-05 : 16:27:33
|
| Guys,I have a recursive table where every record has a child ID, a parent ID and an active flag.If I take a record from, let's say third level I have to check that its parents at level one and two are active. If they aren't I shouldn't return anything.I struggled somewhat with a recursive CTE, but not to the point that I solved this question.Ideas anyone?Nimda |
|
|
consultor.dwh
Starting Member
6 Posts |
Posted - 2011-12-05 : 17:15:30
|
| Maybe recurseive CTE is overkill. You could try to create a query with parent and grandparent. Something like this:select gp.childId gparent ,gp.flag gparent_flag ,p.childid parent ,p.flag parent_flag ,c.childid childfrom table cjoin table p on c.parentid = p.childidjoin table gp on p.parentid = gp.childidThen you can find in this query if your child row has parent and grand parent rows active.This query is using "Join", so, only retrieves rows with parents and grand parents. If orphan rows apply to your requerment, then you will need to change the join to LEFT JOIN |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-05 : 23:42:42
|
quote: Originally posted by consultor.dwh Maybe recurseive CTE is overkill. You could try to create a query with parent and grandparent. Something like this:select gp.childId gparent ,gp.flag gparent_flag ,p.childid parent ,p.flag parent_flag ,c.childid childfrom table cjoin table p on c.parentid = p.childidjoin table gp on p.parentid = gp.childidThen you can find in this query if your child row has parent and grand parent rows active.This query is using "Join", so, only retrieves rows with parents and grand parents. If orphan rows apply to your requerment, then you will need to change the join to LEFT JOIN
this is not scalable. like if user chooses a level more than third it wont traverse till the top and check all their active statusso you've to use some kind of recursive mechanism ie either a loop or recursive cte------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-05 : 23:52:04
|
quote: Originally posted by nimda Guys,I have a recursive table where every record has a child ID, a parent ID and an active flag.If I take a record from, let's say third level I have to check that its parents at level one and two are active. If they aren't I shouldn't return anything.I struggled somewhat with a recursive CTE, but not to the point that I solved this question.Ideas anyone?Nimda
can you post your used query. i feel like this is a straight forward problem.anyways here's a simple represenataion of your scenariocreate table #hierarchy(childid int identity(1,1),parentid int,active bit)insert #hierarchy (parentid,active)values(NULL,1),(1,1),(null,1),(null,1),(3,1),(5,1),(2,1),(7,0)declare @child intset @child=8--pass any value here;with cte (id,parent,active)as(select *from #hierarchywhere childid=@childunion allselect h.*from cte cjoin #hierarchy hon h.childid = c.parent)select *from(select * ,COUNT(case when active=0 then 1 else null end) over() as cntinactfrom cte)twhere cntinact=0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
nimda
Starting Member
4 Posts |
Posted - 2011-12-07 : 05:23:57
|
This is my query. I'll appreciate your help!with UnitResolved (unit_id, parent_id, unit_name, start_date, end_date, is_deleted, depth) as ( select ou.unit_id , ou.parent_id , ou.unit_name , ou.start_date , ou.end_date , ou.is_deleted , 0 [Depth] from dbo.organizational_units ou where ou.parent_id = '-1' union all select ou.unit_id ,ou.parent_id ,ou.unit_name , ou.start_date , ou.end_date , ou.is_deleted , Depth+1 from unitresolved ur join organizational_units ou on ou.parent_id = ur.unit_id)--drop table organizational_units_resolvedselect distinct *into organizational_units_resolvedfrom UnitResolvedOPTION (MAXRECURSION 10000) I will try your solution Visakh. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-07 : 05:33:39
|
| try and let me know------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|