Thursday, November 24, 2011

Count multilevel data

Helo to all I have a table in sql server 2005
Suppose Goldy join kshama   kshama join Harsh  Harsh join  Gaurav and Gaurav join Moni
Now i want to calculate total joinning of Goldy
according to me it should come 4 of Goldy   3 of Kshama  2 of Harsh and 1 Of Gaurav
Now what will be the query
My table is like
Now To achieve this goal I will use With Clause
with emct(id,parentid,name1,usercode,lvel) as
(
select id,parentid,name1,usercode,0  from multilevel 
where id =1(change your id from here)

union all

select main.id,main.parentid,main.name1,main.usercode,lvel +1
from multilevel as main join emct as temp on main.parentid=temp.id

)
select id,parentid,name1,usercode,lvel from emct;
go



No comments:

Post a Comment