我在vs2017 see here中创建了一个平板电脑报告。每个balanceindicator都有一个父键。对于我去年年底的总和"字段我已通过parentKey为BalanceIndicatorKey组配置递归和。在这一点上,一切都很好,并且完美无缺。现在我已经通过organizationKey添加了一个列组。我需要的是考虑组织和平衡指标My report
获得每个组织的递归总和以下是我用于报告的存储过程
LTER PROCEDURE [dbo].[GetFactBalance]
@organizationKey int,
@year int,
@month int
AS
BEGIN
SET NOCOUNT ON;
;with cte_Orgs
as
(
select OrganizationKey,
[Description]
from DimOrganization o
where o.OrganizationKey = @organizationKey
union all
select o.OrganizationKey,
o.[Description]
from cte_Orgs join
DimOrganization o on o.ParentKey = cte_Orgs.OrganizationKey
),
cte_start as (
select
Sum(fa.SumReg) SumReg,
fa.BalanceIndicatorKey,
cte_Orgs.OrganizationKey,
cte_Orgs.Description
from FactBalance fa join
cte_Orgs on cte_Orgs.OrganizationKey = fa.OrganizationKey
where fa.OrganizationKey = cte_Orgs.OrganizationKey
and [Year] = @year-1 and [Month] = 12
group by
fa.BalanceIndicatorKey,
cte_Orgs.OrganizationKey,
cte_Orgs.Description
),
cte_curr as (
select
Sum(fa.SumReg) SumReg,
fa.BalanceIndicatorKey,
cte_Orgs.OrganizationKey,
cte_Orgs.Description
from FactBalance fa join
cte_Orgs on cte_Orgs.OrganizationKey = fa.OrganizationKey
where [Year] = @year and [Month] = @month
group by
fa.BalanceIndicatorKey,
cte_Orgs.OrganizationKey,
cte_Orgs.Description
),
cte_res as (
select
ISNULL(cte_curr.BalanceIndicatorKey, cte_start.BalanceIndicatorKey) BalanceIndicatorKey,
ISNULL(cte_start.OrganizationKey, cte_curr.OrganizationKey) OrganizationKey,
ISNULL(cte_start.Description, cte_curr.Description) [OrgName],
cte_start.SumReg SumRegStart,
cte_curr.SumReg SumReg
from cte_curr full join cte_start on cte_curr.BalanceIndicatorKey = cte_start.BalanceIndicatorKey
and cte_curr.OrganizationKey = cte_start.OrganizationKey)
select
cte_res.OrganizationKey,
cte_res.OrgName,
bi.BalanceIndicatorKey,
bi.Description [Description],
bi.Code,
bi.ParentKey,
cte_res.SumRegStart,
cte_res.SumReg,
isnull(bi.Level, 0) [Level]
from DimBalanceIndicator bi
full join cte_res on cte_res.BalanceIndicatorKey = bi.BalanceIndicatorKey
END
有什么想法吗?谢谢