父子和销售汇总

发布于 2024-07-29 14:15:38 字数 1260 浏览 5 评论 0原文

我有员工表和名为“管理者”的列。 我想将员工的实际数字和目标数字汇总到经理到总监等,

create table
(
empid int
,Name varchar(255)
,actual decimal(38,2)
,target decimal(38,2)
,managedby int
)

以下是迄今为止所做的事情:

    with emp(Period,Label,[name],Unit,Department,[Group],Division,Managed_By,Manager,Actual,Target) as
(
select 
a.DW_Time_key Period
,Label
,b.name
,b.Unit
,b.Department
,b.[Group]
,b.Division
,b.Managed_By
,b.Manager
,sum(a.Actual) Actual
,sum(a.Budget) Target
from 
KPI_EDW.dbo.Fact_ASO_SCORECARD a
,KPI_EDW.dbo.DIM_Employee b 
where
a.DW_EMPLOYEE_KEY = b.DW_EMPLOYEE_KEY 
and DW_KPI_KEY = 1008
and DW_Time_key = 200906
--and DW_Division_KEY = 1003
group by
a.DW_Time_key
,b.name
,b.Unit
,b.Department
,b.[Group]
,b.Division 
,b.Managed_By
,b.Manager
,b.Label
)
,CTE (Label,[Name],[Manager], Managed_By,Actual,Target,[level]) as
(
select cast(Label as int) Label,[Name],[Manager], null,Actual,Target,0
from emp
where cast(Label as int) = '582'

union all

select 
cast(h2.Label as int) Label
,h2.[Name]
,h2.[Manager]
,cast(h2.Managed_By as int) Managed_By 
,h2.Actual
,h2.Target
,[level] + 1
from emp h2
inner join cte
on cast(h2.Managed_By as int) = cast(cte.Label as int)
)


select *
from cte
order by [level]

提前致谢

I have table of employees and column called managedby.
I want to roll actual and target figures up from employee to lead to manager to director and etc.

create table
(
empid int
,Name varchar(255)
,actual decimal(38,2)
,target decimal(38,2)
,managedby int
)

and below is what have done so far:

    with emp(Period,Label,[name],Unit,Department,[Group],Division,Managed_By,Manager,Actual,Target) as
(
select 
a.DW_Time_key Period
,Label
,b.name
,b.Unit
,b.Department
,b.[Group]
,b.Division
,b.Managed_By
,b.Manager
,sum(a.Actual) Actual
,sum(a.Budget) Target
from 
KPI_EDW.dbo.Fact_ASO_SCORECARD a
,KPI_EDW.dbo.DIM_Employee b 
where
a.DW_EMPLOYEE_KEY = b.DW_EMPLOYEE_KEY 
and DW_KPI_KEY = 1008
and DW_Time_key = 200906
--and DW_Division_KEY = 1003
group by
a.DW_Time_key
,b.name
,b.Unit
,b.Department
,b.[Group]
,b.Division 
,b.Managed_By
,b.Manager
,b.Label
)
,CTE (Label,[Name],[Manager], Managed_By,Actual,Target,[level]) as
(
select cast(Label as int) Label,[Name],[Manager], null,Actual,Target,0
from emp
where cast(Label as int) = '582'

union all

select 
cast(h2.Label as int) Label
,h2.[Name]
,h2.[Manager]
,cast(h2.Managed_By as int) Managed_By 
,h2.Actual
,h2.Target
,[level] + 1
from emp h2
inner join cte
on cast(h2.Managed_By as int) = cast(cte.Label as int)
)


select *
from cte
order by [level]

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓦然回首 2024-08-05 14:15:38

听起来您需要一种方法来汇总某个经理下的所有员工以及他们下的每个人的列表的实际值和目标值,依此类推,以获得整个分支机构的总计。 那是对的吗? 我在 SQL Server 中编写了一个递归函数来执行此操作。 如果我正确理解了问题,我可以发布它。

这里是:

CREATE FUNCTION [dbo].[ListStaff] (@Supv int) 
--Recursively returns tree of managed staff
--select * from ListStaff(5)
RETURNS @result TABLE
    (empid int ,
    Name varchar(26),
    managedby int,
    SupvName varchar(26),
    NestLev int 
 )
AS
BEGIN
    DECLARE @empid int, @Name char(26), @managedby int, @SupvName char(26)

    SELECT @SupvName = supv.Name, @managedby = emp.managedby, @Name = emp.Name
    FROM yourTable emp LEFT JOIN yourTable supv
    ON emp.managedby = supv.empid
    WHERE emp.empid = @Supv
    -----------------------
      INSERT INTO @result
      VALUES (@Supv, @Name, @managedby, @SupvName, @@NESTLEVEL)
    -----------------------
    SET @empid = (SELECT MIN(emp.empid)     
    FROM yourTable emp LEFT JOIN yourTable supv
    ON emp.managedby = supv.empid
    WHERE emp.managedby = @Supv)

    WHILE @empid IS NOT NULL
    BEGIN
        IF @@NESTLEVEL < 32
        IF @empid <> @Supv
        INSERT INTO @result
        SELECT * FROM ListStaff(@empid)

        SET @empid = (SELECT MIN(emp.empid) 
        FROM yourTable emp LEFT JOIN yourTable supv
        ON emp.managedby = supv.empid
        WHERE emp.managedby = @Supv AND emp.empid > @empid)
    END
RETURN
END

/* This draws the organizartion chart in a formatted tree.

select CONVERT(CHAR(1),nestLev) + REPLICATE('-', nestLev * 4 - 4) + Name + ' is supervised by ' + SupvName
FROM ListStaff(@bossid)

*/

/* link the results back to your table to get values like this:
SELECT *
FROM ListStaff(5) supv
INNER JOIN yourTable
ON supv.empid = yourTable.empid
*/

It sounds like you need a way to sum the actual and target values for a list of all the employees under a certain manager as well as everyone under them, and so on, to get the totals for an entire branch. Is that correct? I wrote a recursive function to do that in SQL Server. I can post it if I am understanding the problem correctly.

Here it is:

CREATE FUNCTION [dbo].[ListStaff] (@Supv int) 
--Recursively returns tree of managed staff
--select * from ListStaff(5)
RETURNS @result TABLE
    (empid int ,
    Name varchar(26),
    managedby int,
    SupvName varchar(26),
    NestLev int 
 )
AS
BEGIN
    DECLARE @empid int, @Name char(26), @managedby int, @SupvName char(26)

    SELECT @SupvName = supv.Name, @managedby = emp.managedby, @Name = emp.Name
    FROM yourTable emp LEFT JOIN yourTable supv
    ON emp.managedby = supv.empid
    WHERE emp.empid = @Supv
    -----------------------
      INSERT INTO @result
      VALUES (@Supv, @Name, @managedby, @SupvName, @@NESTLEVEL)
    -----------------------
    SET @empid = (SELECT MIN(emp.empid)     
    FROM yourTable emp LEFT JOIN yourTable supv
    ON emp.managedby = supv.empid
    WHERE emp.managedby = @Supv)

    WHILE @empid IS NOT NULL
    BEGIN
        IF @@NESTLEVEL < 32
        IF @empid <> @Supv
        INSERT INTO @result
        SELECT * FROM ListStaff(@empid)

        SET @empid = (SELECT MIN(emp.empid) 
        FROM yourTable emp LEFT JOIN yourTable supv
        ON emp.managedby = supv.empid
        WHERE emp.managedby = @Supv AND emp.empid > @empid)
    END
RETURN
END

/* This draws the organizartion chart in a formatted tree.

select CONVERT(CHAR(1),nestLev) + REPLICATE('-', nestLev * 4 - 4) + Name + ' is supervised by ' + SupvName
FROM ListStaff(@bossid)

*/

/* link the results back to your table to get values like this:
SELECT *
FROM ListStaff(5) supv
INNER JOIN yourTable
ON supv.empid = yourTable.empid
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文