如何显示将行转换为具有总计的列

时间:2017-06-09 12:08:02

标签: sql-server

提前感谢您的帮助!

我想要完成的是:我在同一个部门有一张不同薪资等级的表。如何在列中显示薪资等级,每列的计数如下,然后显示每个部门的工资等级的GRAND。这就是我所拥有的:

Dept_Num    Dept_Name   Salary_Grade    TOTAL_SalaryGrade
2005           Mrktg              1        39
2005           Mrktg              2        26
2005           Mrktg              3         5
2010           Payroll            1        20
2010           Payroll            2         8
2015           Acct               1         6
2015           Acct               3         6

Dept_Num    Dept_Name   Salary_Grade 1  Salary_Grade 2  Salary_Grade 3  
2005          Mrktg            39          26              5    
2010          Payroll          20           8              0    
2015          Acct              6           0              6    
TOTAL_SalaryGrade              65          34             11



SELECT[Dept_Num]
  ,[Dept_Name]
  [Salary_Grades],
  [TOTAL_Salary_Grades],  
 FROM [Employees]

感谢您提供任何帮助!

1 个答案:

答案 0 :(得分:0)

尝试如下:

CREATE TABLE EMPLOYEES  (Dept_Num  INT,  Dept_Name CHAR(20),  Salary_Grade  INT,  TOTAL_SalaryGrade INT )
INSERT INTO EMPLOYEES VALUES (2005,'Mrktg'  ,1,39 )
INSERT INTO EMPLOYEES VALUES (2005,'Mrktg'  ,2,26 )
INSERT INTO EMPLOYEES VALUES (2005,'Mrktg'  ,3, 5 )
INSERT INTO EMPLOYEES VALUES (2010,'Payroll',1,20 )
INSERT INTO EMPLOYEES VALUES (2010,'Payroll',2, 8 )
INSERT INTO EMPLOYEES VALUES (2015,'Acct'   ,1, 6 )
INSERT INTO EMPLOYEES VALUES (2015,'Acct'   ,3, 6 )

(SELECT convert(varchar,Dept_Num) [Dept_Num],Dept_Name, isnull([1],0) [Salary_Grade 1],isnull([2],0) [Salary_Grade 2],isnull([3],0) [Salary_Grade 3]  from 
            (
                select Dept_Num
                    , Dept_Name
                    , TOTAL_SalaryGrade
                    , Salary_Grade
                from EMPLOYEES
           ) x
            pivot 
            (
                 SUM(TOTAL_SalaryGrade)
                for Salary_Grade in ([1],[2],[3])
            ) p )

union all
select 'TOTAL_SalaryGrade','',[1],[2],[3]  from
(SELECT SUM (TOTAL_SalaryGrade) as total,Salary_Grade     FROM EMPLOYEES GROUP BY Salary_Grade ) b
pivot
(sum(total) for  Salary_Grade in ([1],[2],[3])) q

结果是:

*-----------------*----------*------------------*---------------*---------------*
|Dept_Num         | Dept_Name|  Salary_Grade 1  |Salary_Grade 2 |Salary_Grade 3|
*-----------------*----------*------------------*---------------*---------------
|2015             | Acct     |     6            |    0          |  6           |
*-----------------*----------*------------------*---------------*---------------
|2005             | Mrktg    |     39           |  26           |   5          |
*-----------------*----------*------------------*---------------*---------------
|2010             | Payroll  |     20           |  8            |  0           |
*-----------------*----------*------------------*---------------*---------------
|TOTAL_SalaryGrade|          |     65           |  34           |11            |
*-----------------*----------*------------------*---------------*---------------