我的递归查询需要一些帮助,以获取我的 SSRS 报告的直接计数(所有成员(儿童)直接)和总计数(所有团队成员)。
这是我当前的查询和结果集。
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
当前结果集
AId Name Upline UId HLevel ContractDate DirectCount
-----------------------------------------------------------------------
A049 King Bori Cindy Hoss A001 0 8/29/2012 5
A052 Kac Marque King Bori A049 1 11/6/2012 0
A050 Joseph Moto King Bori A049 1 10/9/2012 1
A059 Nancy Ante King Bori A049 1 3/27/2013 1
A053 Kathy May King Bori A049 1 11/15/2012 2
A057 Robert Murphy King Bori A049 1 2/12/2013 1
A051 Andy Jane Joseph Moto A050 2 2/14/2013 0
A060 Arian Colle Nancy Ante A059 2 3/26/2013 0
A058 Phil Hunk Robert Murphy A057 2 3/21/2013 0
A055 Rea Wane Kathy May A053 2 2/20/2013 1
A054 Gabby Orez Kathy May A053 2 12/7/2012 0
A056 Steve Wells Rea Wane A055 3 3/25/2013 0
我需要更改上述查询以根据合同日期获取直接计数(所有成员(儿童)直接)和 TotalTeam 计数
例如,例如合同日期在 2013 年 3 月 1 日和 2013 年 3 月 31 日之间。我需要得到以下结果集。
我需要合并 contractDate 的参数(以便他们可以获得范围,或者如果它为空,那么他们将获得所有记录和计数。
例如(@Begindate 和@Enddate 之间的ContractDate)或((@Begindate 为空)和(@enddate 为空))
AId Name Upline UId HLevel ContractDate DirectCount TotalTeam
---------------------------------------------------------------------------------
A049 King Bori Cindy Hoss A001 0 8/29/2012 1 4
A052 Kac Marque King Bori A049 1 11/6/2012 0 0
A050 Joseph Moto King Bori A049 1 10/9/2012 0 0
A059 Nancy Ante King Bori A049 1 3/27/2013 1 1
A053 Kathy May King Bori A049 1 11/15/2012 0 0
A057 Robert Murphy King Bori A049 1 2/12/2013 1 1
A051 Andy Jane Joseph Moto A050 2 2/14/2013 0 0
A060 Arian Colle Nancy Ante A059 2 3/26/2013 0 0
A058 Phil Hunk Robert Murphy A057 2 3/21/2013 0 0
A055 Rea Wane Kathy May A053 2 2/20/2013 1 1
A054 Gabby Orez Kathy May A053 2 12/7/2012 0 0
A056 Steve Wells Rea Wane A055 3 3/25/2013 0 0
提前致谢。