SQL Server 递归找出父子记录,并标统计当前所在层级用户数

来源:互联网 发布:淘宝网65平开窗传动器 编辑:程序博客网 时间:2024/06/04 18:13
select identity(int,1,1)userid,* into #t from( select 'name1'LoginName,0 FatherUserID union all select 'name2',     1 union all select 'name3',     1 union all select 'name4',     2 union all select 'name5',     2 union all select 'name6',     4 union all select 'name7',     4 )a   ;with temp_users as( select UserID,LoginName,FatherUserID,1 as levle from #t where UserID=1 union allselect  A.UserID,A.LoginName,A.FatherUserID,B.levle+1 from #t A,temp_users B where A.FatherUserID=B.UserID ) select count(userid) as count,[level] from temp_users  group by  [level] 
count level  1 01 31 15 2
 
	
				
		
原创粉丝点击