显示每个经理下的所有员工角色

时间:2017-04-27 07:52:41

标签: sql sql-server sql-server-2008

在SQL Server 2008中,我有一个包含5列的表tblStock:

Manager,Employee,Value1,Value2,Value3。

样本数据将是,

enter image description here

我编写了以下代码来获取上面的示例输出:

   Highcharts.chart('containerChart', {
        chart: {
            type: 'bar',
            marginLeft: 0,
            marginTop: 100
        },
        title: {
            text: 'Reject Category',
            align: 'left',
            x: 30,
            y: 35,
            style: {
                color: '#666666',
                fontWeight: 'normal',
                fontSize: 12,
                fontFamily: 'Lucida Grande,Lucida Sans Unicode, Arial, Helvetica, sans-serif',
            }
        },
        tooltip: {
            enabled: false,
            valueSuffix: ' %',

            }
        },
        exporting: {
            enabled: false,
        },
        credits: {
            enabled: false,
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: false
                },

            },
            series: {
                pointWidth: 25,

                }, states: {
                    hover: {
                        enabled: false
                    }
                }
            },

        },
        xAxis: [{
            categories: arrcategory,
            width: 100,
            left: 140,
            height: 34 * ctglength,
            lineWidth: 0,
            minorGridLineWidth: 0,
            minorTickLength: 0,
            // tickLength: 0,
            title: {
                text: null,
            },
            labels: {
                overflow: 'justify',
                enabled: true,
                style: {
                    fontWeight: 'bold',

                }

            },
            gridLineWidth: 0
        }, {
            categories: true,
            linkedTo: 0,
            width: 100,
            height: 34 * ctglength,
            left: 358,
            visible: false
        }],
        yAxis: [{
            min: 0,
            max: 100,
            width: 400,
            left: 140,
            //gridLineWidth: 0,
            labels: {
                overflow: 'justify',
                enabled: false
            },
            title: {
                text: '# of Transactions',
                "textAlign": 'right',
                "rotation": 0,
                x: 15,
                y: -310
            },

        }, {
            min: 0,
            max: 100,
            left: 700,
            width: 400,
            // gridLineWidth: 0,
            labels: {
                overflow: 'justify',
                enabled: false
            },
            title: {
                text: '$ in Premium',
                "textAlign": 'right',
                "rotation": 0,
                x: 10,
                y: -330
            },
        }],
        series: [{
            data: arrdata1,
            color: '#E95E4F',
            showInLegend: false,
            //enableMouseTracking: false,
            dataLabels: {
                overflow: false,
                enabled: true,
                crop: false,



        }, {
            data: arrdata2,
            color: '#E95E4F',
            showInLegend: false,
            //enableMouseTracking: false,
            dataLabels: {
                overflow: false,
                enabled: true,
                crop: false,

            },
            yAxis: 1,
            xAxis: 1
        }]
    });

我想从上表中获得以下输出,

enter image description here

在这个输出中,我提到的绿色行是属于特定管理者的员工的总和。

任何人都知道这方面的解决方案。

5 个答案:

答案 0 :(得分:2)

这是一种方法:

SELECT Name, Role, Value1, Value2, Value3
FROM
(
SELECT  Manager As Name, 
        'Manager' As Role, 
        SUM(Value1) As Value1, 
        SUM(Value2) AS Value2, 
        SUM(Value3)  AS Value3,
        Manager
FROM #tblDepartment 
GROUP BY Manager

UNION ALL

SELECT Employee As Name,
        'Employee' As Role, 
        Value1, 
        Value2, 
        Value3,
        Manager
FROM #tblDepartment 
) t
ORDER BY Manager,Role DESC, Name

这与您的订单不完全相同,但员工都在正确的经理之下。

See a live demo on rextester.

答案 1 :(得分:2)

获得结果的另一种方法

WITH mCTE AS (
   SELECT a.Manager, a.Manager AS Employee, 'Manager' AS Role, SUM(a.Value1) AS Value1, SUM(a.Value2) AS Value2, SUM(a.Value3) AS Value3, 1 AS r
   FROM #tblDepartment AS a
   GROUP BY a.Manager

   UNION ALL

   SELECT a.Manager, a.Employee, 'Employee' AS Role, a.Value1, a.Value2, a.Value3, 2 AS r
   FROM #tblDepartment AS a
)

SELECT Employee, Role, Value1, Value2, Value3
FROM mCTE
ORDER BY Manager, r

答案 2 :(得分:1)

考虑到您的订单,这是最接近您的结果:

select Name, Role, Value1, Value2, Value3
from (
       select t.MgrId, t.manager as Name, 'Manager' as Role, sum(t.Value1) Value1, sum(t.Value2) Value2, sum(t.Value3) Value3
       from (select *, dense_rank () OVER(ORDER BY Manager desc) AS MgrId from  #tblDepartment ) t
       group by t.MgrId, t.manager

       union all

       select t.MgrId, t.employee as Name, 'Employee' as Role, sum(t.Value1) Value1, sum(t.Value2) Value2, sum(t.Value3) Value3
       from (select *, dense_rank () OVER(ORDER BY Manager desc) AS MgrId from  #tblDepartment ) t
       group by  t.MgrId, t.employee
   ) t 
order by t.MgrId, t.Role desc

您可以查看工作版本here。 排名函数从SQL Server 2008开始提供,因此它也适用于您。

答案 3 :(得分:1)

试试这个:

 declare @tb table(manager varchar(50),employee varchar(50), value1 int,value2 int,value3 int)
 insert into @tb
 SELECT 'Theva' Manager,  'Lawlrence' Employee, 10 Value1, 20 Value2, 60 Value3 
    UNION ALL 
    SELECT 'Theva', 'David', 20, 35, 42 
    UNION ALL 
    SELECT 'Theva', 'Ragav', 45, 35, 86 
    UNION ALL 
    SELECT 'Prem', 'Vino', 69, 99, 45 
    UNION ALL 
    SELECT 'Prem', 'Lara', 27, 99, 45 
    UNION ALL 
    SELECT 'Anzal', 'Ranjani', 65, 55, 12 
    UNION ALL 
    SELECT 'Anzal', 'Priya', 55, 47, 89 
    UNION ALL 
    SELECT 'Anzal', 'Vinoth', 98, 53, 56 
    UNION ALL 
    SELECT 'Rafeek', 'Ashok', 48, 75, 45

    select  case when len(employee)>0 then employee else a.manager end as Name,case when len(employee)>0 then 'Employee' else 'Manager' end as [Role],b.value1,b.value2,b.value3 from
    (select distinct manager,'Manager' as Role from @tb
    union all
    select employee,'employee' as Role from @tb) as a



    left join


    (select distinct a.manager,employee,value1,value2,value3 from
    (select manager,'' as employee,sum(value1)value1,sum(value2)value2,sum(value3)value3 from @tb group by manager
    union all
    select a.manager,b.employee,b.value1,b.value2,b.value3 from
    (select manager,sum(value1)value1,sum(value2)value2,sum(value3)value3 from @tb group by manager) as a
    left join
    (select * from @tb) as b
    on a.manager = b.manager) as a) as b
    on a.manager = b.manager where value1 is not null

结果:

Anzal   Manager     218 155 157
Priya   Employee    55  47  89
Ranjani Employee    65  55  12
Vinoth  Employee    98  53  56
Prem    Manager     96  198 90
Lara    Employee    27  99  45
Vino    Employee    69  99  45
Rafeek  Manager     48  75  45
Ashok   Employee    48  75  45
Theva   Manager     75  90  188
David   Employee    20  35  42
Lawlrence Employee  10  20  60
Ragav   Employee    45  35  86

答案 4 :(得分:1)

这可能不是OP的理想答案,我尝试使用SQL 2012重新创建它,这就是我想出来的。

    IF OBJECT_ID('tempdb..#tblDepartment') IS NOT NULL DROP TABLE #tblDepartment 

    SELECT * INTO #tblDepartment
    FROM
    (
        SELECT 'Theva' Manager,  'Lawlrence' Employee, 10 Value1, 20 Value2, 60 Value3 
        UNION ALL 
        SELECT 'Theva', 'David', 20, 35, 42 
        UNION ALL 
        SELECT 'Theva', 'Ragav', 45, 35, 86 
        UNION ALL 
        SELECT 'Prem', 'Vino', 69, 99, 45 
        UNION ALL 
        SELECT 'Prem', 'Lara', 27, 99, 45 
        UNION ALL 
        SELECT 'Anzal', 'Ranjani', 65, 55, 12 
        UNION ALL 
        SELECT 'Anzal', 'Priya', 55, 47, 89 
        UNION ALL 
        SELECT 'Anzal', 'Vinoth', 98, 53, 56 
        UNION ALL 
        SELECT 'Rafeek', 'Ashok', 48, 75, 45
    )TAB

    DECLARE @T AS TABLE
        (
          ID INT IDENTITY(1, 1)
                 PRIMARY KEY ,
          NAME VARCHAR(50) ,
          Role VARCHAR(50) ,
          Value1 INT ,
          Value2 INT ,
          Value3 INT
        );
    WITH    CTE
              AS ( SELECT   Manager ,
                            Employee ,
                            SUM(Value1) AS Value1 ,
                            SUM(Value2) AS Value2 ,
                            SUM(Value3) AS Value3
                   FROM     #tblDepartment
                   GROUP BY Manager ,
                            Employee
                            WITH ROLLUP
                 )
        INSERT  INTO @T
                ( NAME, Role, Value1, Value2, Value3 )
    SELECT  Name ,
            Role ,
            Value1 ,
            Value2 ,
            Value3
    FROM    ( SELECT    ISNULL(Employee, Manager) NAME ,
                        IIF(Employee IS NULL, 'Manager', 'Employee') ROLE ,
                        Value1 ,
                        Value2 ,
                        Value3
              FROM      CTE
            ) T
    WHERE   t.NAME IS NOT NULL

    SELECT  Name ,
            Role ,
            Value1 ,
            Value2 ,
            Value3 FROM @T
    ORDER BY ID DESC

结果:

        Name                  Role               Value1      Value2      Value3
        --------------------- ------------------ ----------- ----------- -----------
        Theva                 Manager            75          90          188
        Ragav                 Employee           45          35          86
        Lawlrence             Employee           10          20          60
        David                 Employee           20          35          42
        Rafeek                Manager            48          75          45
        Ashok                 Employee           48          75          45
        Prem                  Manager            96          198         90
        Vino                  Employee           69          99          45
        Lara                  Employee           27          99          45
        Anzal                 Manager            218         155         157
        Vinoth                Employee           98          53          56
        Ranjani               Employee           65          55          12
        Priya                 Employee           55          47          89