SQL Server 2008/Reporting Services查询
问题描述:
我的递归查询需要一些帮助,以获得我的SSRS报告的直接计数(所有成员(子))和总数(所有团队成员)。SQL Server 2008/Reporting Services查询
这是我目前的查询和结果集。
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年1月3日至2013年3月31日期间的合同日期。我需要获得以下结果集。
我需要把对contractDate参数(使他们能够得到的范围内,或者如果它为null,则他们得到所有的记录和计数。
如(@BeginDate和@EndDate之间ContractDate)或((@BeginDate为空)和(@EndDate为null))
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
在此先感谢。
答
我不能肯定,但你在你的递归CTE做一个人一个明确的上市,这将限制范围仅限于那个人和他们的父母ONL Y.除非你在一组数百万条记录上进行递归,否则它应该能够在正则表达式的底部处理谓词,而不是在递归CTE中处理它自己。假设你正在处理适当的最大递归级别。对于您的合同日期,只需将该表格留在链接递归的底部。除非你需要先获得他们的水平。在那种情况下,我会在第一个cte中获取这些数据,然后列出第二个数据,然后再做这个数据。
这里是一个简单的例子,我做了包括联合销售,基本上我形成了递归,而不是id特定的,我发现最大递归(如果你想离开那部分)找到最低叶级,然后我执行所需的结束谓词。我希望这有帮助。很多时候,我看到人们在递归CTE中列出谓词,这会限制它们的范围,记住递归本质上限制了n次以上的层次。你可以在该点之前和之后获得你需要的数据,但是在那里做谓词会限制这个范围导致的地方。
Declare @table table (PersonId int identity, PersonName varchar(512), Account int, ParentId int, Orders int);
insert into @Table values ('Brett', 1, NULL, 1000),('John', 1, 1, 100),('James', 1, 1, 200),('Beth', 1, 2, 300),('John2', 2, 4, 400);
select
PersonID
, PersonName
, Account
, ParentID
from @Table
; with recursion as
(
select
t1.PersonID
, t1.PersonName
, t1.Account
--, t1.ParentID
, cast(isnull(t2.PersonName, '')
+ Case when t2.PersonName is not null then '\' + t1.PersonName else t1.PersonName end
as varchar(255)) as fullheirarchy
, 1 as pos
, cast(t1.orders +
isnull(t2.orders,0) -- if the parent has no orders than zero
as int) as Orders
from @Table t1
left join @Table t2 on t1.ParentId = t2.PersonId
union all
select
t.PersonID
, t.PersonName
, t.Account
--, t.ParentID
, cast(r.fullheirarchy + '\' + t.PersonName as varchar(255))
, pos + 1 -- increases
, r.orders + t.orders
from @Table t
join recursion r on t.ParentId = r.PersonId
)
, b as
(
select *, max(pos) over(partition by PersonID) as maxrec -- I find the maximum occurrence of position by person
from recursion
)
select *
from b
where pos = maxrec -- finds the furthest down tree
-- and Account = 2 -- I could find just someone from a different department