【问题标题】:SQL Server 2008 / Reporting Services querySQL Server 2008 / Reporting Services 查询
【发布时间】:2013-04-08 15:17:31
【问题描述】:

我的递归查询需要一些帮助,以获取我的 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 之间的合同日期)或((@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

提前致谢。

【问题讨论】:

    标签: sql-server tsql reporting-services


    【解决方案1】:

    我不确定,但您在递归 CTE 中明确列出了一个人,这会将范围仅限于该人及其父母。除非您对数百万条记录的起始集进行递归,否则它应该能够处理正则表达式底部的谓词,而不是递归 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-07
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2010-12-23
      • 1970-01-01
      相关资源
      最近更新 更多