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 all select 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 0 1 3 1 1 5 2
SQL Server 递归找出父子记录,并标统计当前所在层级用户数
最新推荐文章于 2018-07-24 11:06:19 发布