Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create table chiefTree(
- id int NOT NULL
- ,parent_id int NULL
- ,name varchar(255) NOT NULL
- )
- insert value chiefTree
- (id,parent_id,name) primary key
- values(1,null,'Начальник1')
- ,(1,1,'Начальник2')
- ,(1,2,'Начальник3')
- ,(1,3,'Начальник4')
- ,(1,4,'Начальник5')
- create users(
- id int identity(1,1) NOT NULL
- ,parent_id int NOT NULL REFERENCES chiefTree (id)
- ,name varchar(255) not null
- )
- select
- t1.name as [Уровень1]
- ,t2.name as [Уровень2]
- ,t2.name as [Уровень3]
- ,t3.name as [Уровень4]
- ,t4.name as [Уровень5]
- from chiefTree t1
- left join chiefTree t2 on t2.id= t1.parent_id
- left join chiefTree t3 on t3.id= t2.parent_id
- left join chiefTree t4 on t4.id= t3.parent_id
- left join chiefTree t5 on t5.id= t4.parent_id
- where t1.parent_id = null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement