Click here to Skip to main content
15,890,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to search title '%p%'. if i check title ,parent tile and status as parent and child level. if the searched title parent visible is true then show the child Title.

CREATE TABLE CTE(
Title VARCHAR(20),
ParentTitle VARCHAR(20),
visible VARCHAR(20),
)

INSERT INTO CTE
VALUES
('p1', 'Home', 'TRUE'),
('p1.1', 'p1', 'TRUE'),
('p1.2', 'p1', 'FALSE'),
('p1.3', 'p1.2', 'TRUE'),
('p1.3.1', 'p1.3', 'TRUE'),
('p2', 'Home', 'TRUE'),
('p2.1', 'p2', 'TRUE'),
('p2.2', 'p2.1', 'FALSE'),
('P3', 'Home', 'TRUE'),
('p3.1', 'p3', 'TRUE'),
('P3.1.1', 'p3.1', 'FALSE')



I need output like

title
p1
p1.1
p2
p2.1
P3
p3.1

What I have tried:

C#
Select b.Title,b.ParentTitle from CTE  B
 left join  CTE C   ON C.ParentTitle=b.Title AND b.visible='TRue'
 where b.ParentTitle in(Select a.ParentTitle from CTE A  join CTE B on a.Title=b.ParentTitle  and b.visible='false')

i tried this query. But i can't get correct answer..
Posted
Updated 3-Nov-16 4:25am
v2

1 solution

Hi,

You need to apply recursive cte . Try below script for your output.


;with tbl as (
select title ,ParentTitle from cte where ParentTitle='Home'
union all
select a.title,a.ParentTitle from cte a inner join tbl b on
a.ParentTitle=b.title
where a.visible='true'
)

select title from tbl order by title

Here tbl is cte.

Thanks
 
Share this answer
 

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