我希望你能帮助我
我在这里有一个问题,一直试图解决这个问题,但无法弄清楚如何解决。我有一个简单的表格,表示代表及其领导者的周期(时间指标),如下所示: REPID | LeaderID | CycleID
我需要为每个领导者计算他们下面的整个树只有两次特定状态,因为公式是: (所有代表在领导者之下,状态为1或2) - (所有代表在上一个周期的领导者之下,状态为7)
我的代码如下:
update ccp
set ccp.Total_reps =
(
select isnull(COUNT(*),0)
from dbo.MCP_Reps_by_cycle ccp_total_reps
join dbo.MCP_Compensation_Plan cp_total_reps on cp_total_reps.ID_Compensation_Plan = ccp_total_reps.ID_Compensation_Plan and cp_total_reps.CycleID = @previous_cycle and cp_total_reps.Definitive = 1 and cp_total_reps.Active = 1
join dbo.F_Get_reps(ccp.RepID, @previous_cycle) r on r.RepID = ccp_total_reps.RepID
where ccp_total_reps.StatusDetailID in(1,2) and ccp_total_reps.Active = 1
)
-
(
select isnull(COUNT(*),0)
from dbo.MCP_Reps_by_cycle ccp_total_reps
join dbo.MCP_Compensation_Plan cp_total_reps on cp_total_reps.ID_Compensation_Plan = ccp_total_reps.ID_Compensation_Plan and cp_total_reps.CycleID = @previous_cycle_two and cp_total_reps.Definitive = 1 and cp_total_reps.Active = 1
join dbo.F_Get_reps(ccp.RepID, @previous_cycle_two) r on r.RepID = ccp_total_reps.RepID
where ccp_total_reps.StatusDetailID = 7 and ccp_total_reps.Active = 1
)
from dbo.MCP_Reps_by_cycle ccp
join dbo.MCP_Compensation_Plan cp on cp.ID_Compensation_Plan = ccp.ID_Compensation_Plan and cp.ID_Compensation_Plan = @ID_Compensation_Plan and cp.Active = 1
where ccp.Active = 1
F_Get_reps函数的作用是一个简单的递归CTE:
RETURNS @MIT table (RepID int)
as
begin
with rep_network as (
select ccp.RepID
from dbo.MCP_Reps_by_cycle ccp
join dbo.MCP_Compensation_Plan cp on cp.ID_Compensation_Plan = ccp.ID_Compensation_Plan and cp.CycleID = @cycleid and cp.Definitive = 1 and cp.Active = 1
where ccp.Active = 1 and ccp.LeaderID = @repid
union all
select ccp_2.RepID
from dbo.MCP_Reps_by_cycle ccp_2
join dbo.MCP_Compensation_Plan cp_2 on cp_2.ID_Compensation_Plan = ccp_2.ID_Compensation_Plan and cp_2.CycleID = @cycleid and cp_2.Definitive = 1 and cp_2.Active = 1
join rep_network r on r.RepID = ccp_2.LeaderID
where ccp_2.Active = 1
)
insert into @MIT
select RepID
from rep_network
return
end
我知道对函数的调用是问题,但我无法弄清楚如何删除它。
请帮帮我
提前致谢