sqlsql-servert-sqlgroup-byrollup

SQL rollup to add a total row for multiple columns


I am using the employee data set from Kaggle - Dataset

I created a pivot table on top of this for EmployeeClassificationType and EmployeeStatus

with pivotTable as 
(
    select 
        EmployeeClassificationType, [Active], [Future Start], 
        [Leave of Absence], [Terminated for Cause], 
        [Voluntarily Terminated],
        ([Active] + [Future Start] + [Leave of Absence] + [Terminated for Cause] + [Voluntarily Terminated]) as total
    from 
        (select 
             EmployeeStatus, EmployeeClassificationType, count(*) as total
         from  
             employee_data
         group by 
             EmployeeStatus, EmployeeClassificationType) a
    pivot
        (sum(total)
            for employeeStatus in ([Active], [Future Start], [Leave of Absence], [Terminated for Cause], [Voluntarily Terminated])
        ) as pivotTable
)
-- rollup and find the grand total as well as add a row to total 
select * 
from pivotTable

The output looks like this:

Current output

I want to add a row for totals as shown in this screenshot:

Expected output

I have tried using rollup with all columns but its seems to not work

select 
    coalesce(EmployeeClassificationType, 'Grand Total') as EmployeeClassificationType,
    sum(Active) as Active,
    sum([Future Start]) as [Future Start],
    sum([Leave of Absence]) as [Leave of Absence],
    sum([Terminated for Cause]) as [Terminated for Cause],
    sum([Voluntarily Terminated]) as [Voluntarily Terminated],
    sum(total) as 'Grand Total'
from 
    pivotTable
group by 
    rollup(EmployeeClassificationType, Active, [Future Start], [Leave of Absence], [Terminated for Cause]), [Voluntarily Terminated]

or

select 
    *,
    sum(total) as 'Grand Total'
from 
    pivotTable
group by 
    rollup(EmployeeClassificationType, Active, [Future Start], [Leave of Absence], [Terminated for Cause]), [Voluntarily Terminated], total

Output


Solution

  • data

    CREATE TABLE pivotTable (
       EmployeeClassificationType VARCHAR(90)  
      ,Active                     INTEGER  NOT NULL
      ,FutureStart                INTEGER  NOT NULL
      ,LeaveOfAbsence             INTEGER  NOT NULL
      ,TerminatedForCause         INTEGER  NOT NULL
      ,VoluntarilyTerminated      INTEGER  NOT NULL
      ,total                      INTEGER  NOT NULL
    );
    INSERT INTO pivotTable 
    (EmployeeClassificationType,Active,FutureStart,LeaveOfAbsence,TerminatedForCause,VoluntarilyTerminated,total) VALUES 
    ('Full-Time',840,26,21,24,102,1013),
    ('Part-Time',779,20,35,15,105,954),
    ('Temporary',839,23,30,27,114,1033);
    

    query use group by .... with rollup not group by rollup(...)and ISNULL to cover null values for EmployeeClassificationType column

    SELECT
    ISNULL(EmployeeClassificationType,'TOTAL'),
    SUM(Active) Active,
    SUM(FutureStart)  FutureStart,
    SUM(LeaveOfAbsence)   LeaveOfAbsence,
    SUM(TerminatedForCause)  TerminatedForCause,
    SUM(VoluntarilyTerminated)  VoluntarilyTerminated,
    SUM(total) total
    FROM 
    pivotTable
     group 
        by EmployeeClassificationType with rollup;
    

    add above query to your with CTE

    with pivotTable as 
    (
        select 
            EmployeeClassificationType, [Active], [Future Start], 
            [Leave of Absence], [Terminated for Cause], 
            [Voluntarily Terminated],
            ([Active] + [Future Start] + [Leave of Absence] + [Terminated for Cause] + [Voluntarily Terminated]) as total
        from 
            (select 
                 EmployeeStatus, EmployeeClassificationType, count(*) as total
             from  
                 employee_data
             group by 
                 EmployeeStatus, EmployeeClassificationType) a
        pivot
            (sum(total)
                for employeeStatus in ([Active], [Future Start], [Leave of Absence], [Terminated for Cause], [Voluntarily Terminated])
            ) as pivotTable
    )
    
    SELECT
    ISNULL(EmployeeClassificationType,'TOTAL'),
    SUM(Active) Active,
    SUM(FutureStart)  FutureStart,
    SUM(LeaveOfAbsence)   LeaveOfAbsence,
    SUM(TerminatedForCause)  TerminatedForCause,
    SUM(VoluntarilyTerminated)  VoluntarilyTerminated,
    SUM(total) total
    FROM 
    pivotTable
     group 
        by EmployeeClassificationType with rollup;
    

    dbfiddle