Quantcast
Channel: Recursive Query -
Viewing all articles
Browse latest Browse all 3

Recursive Query -

$
0
0

Hi Everyone, 
I am new memeber Please help.
I am using sql server 2008.
I need some help with my recursive query to get a directcount(all members(children) directly) and totalcount(all team members) for my SSRS report.

here is my current query and the result set.

    WITH AgentHierarchy([Name], AId, UId, HLevel, ContractDate) 
    AS (SELECT  FirstName + ' ' + LastName  AS Name, AId, UId, 0 AS HLevel,ContractDate                       
    FROM    tbl_Asso 
    WHERE  (AId ='A049')
    UNION ALL
    SELECT      e.FirstName + ' ' + e.LastName AS Name,
   e.AId, e.UId, eh.HLevel + 1 AS HLevel,
            e.ContractDate
    FROM        tbl_Asso AS e INNER JOIN AgentHierarchy AS eh ON eh.AId = e.UId  )

    SELECT   AId, Name,
(select u.FirstName + ' ' + u.LastName  from tbl_Asso u 
          where u.AId = d.UId ) as Upline,
UId, 
HLevel,  
ContractDate,
(Select count(*)  from tbl_Asso as dc where dc.UId=d.AId) As DirectCount
     FROM    AgentHierarchy AS d
order by HierarchyLevel

the current result set


sud


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images