I have a payroll table that holds dollars by month and hours by month as columns, like so...
Employee | Hours1 | Hours2 | Hours3 | ... | Dollars1 | Dollars2 | Dollars3 |
Andrew | 40 | 40 | 45 | ... | 1000 | 1000 | 1500 |
I would like to unpivot this data to display like this...
Employee | Month | Hours | Dollars
Andrew | 1 | 40 | 1000
Andrew | 2 | 40 | 1000
Andrew | 3 | 45 | 1500
I can get the UNPIVOT command working on either the Hours or Dollars columns separately, but cannot figure out how to UNPIVOT twice and combine the results together. Any help would be appreciated, my code is below...
Employee | Hours1 | Hours2 | Hours3 | ... | Dollars1 | Dollars2 | Dollars3 |
Andrew | 40 | 40 | 45 | ... | 1000 | 1000 | 1500 |
I would like to unpivot this data to display like this...
Employee | Month | Hours | Dollars
Andrew | 1 | 40 | 1000
Andrew | 2 | 40 | 1000
Andrew | 3 | 45 | 1500
I can get the UNPIVOT command working on either the Hours or Dollars columns separately, but cannot figure out how to UNPIVOT twice and combine the results together. Any help would be appreciated, my code is below...
SQL:
SELECT CostCenter, EmployeeID, DataSetID, PayType, SummaryPayType, JobClass, Description, Mnth, MonthHours, MonthDollars
FROM
(SELECT "T_EMPLOYEE_RESULTS"."CostCenter", "T_EMPLOYEE_RESULTS"."EmployeeID", "T_EMPLOYEE_RESULTS"."DataSetID",
"T_PAY_TYPES"."PayType", "T_PAY_TYPES"."SummaryPayType", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours01" AS "Jul",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours02" AS "Aug", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours03" AS "Sept",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours04" AS "Oct", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours05" AS "Nov",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours06" AS "Dec", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours07" AS "Jan",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours08" AS "Feb", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours09" AS "Mar",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours10" AS "Apr", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours11" AS "May",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Hours12" AS "Jun", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars01" AS "Jul",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars02" AS "Aug", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars03" AS "Sept",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars04" AS "Oct", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars05" AS "Nov",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars06" AS "Dec", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars07" AS "Jan",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars08" AS "Feb", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars09" AS "Mar",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars10" AS "Apr", "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars11" AS "May",
"T_EMPLOYEE_RESULTS_MONTH_DETAIL"."Dollars12" AS "Jun", "T_EMPLOYEE_RESULTS"."FacilityID", "T_EMPLOYEE_RESULTS"."JobClass",
"T_JOB_CLASSES"."Description"
FROM (("LG_SYSTEM"."dbo"."T_EMPLOYEE_RESULTS_MONTH_DETAIL" "T_EMPLOYEE_RESULTS_MONTH_DETAIL" INNER JOIN
"LG_SYSTEM"."dbo"."T_EMPLOYEE_RESULTS" "T_EMPLOYEE_RESULTS" ON "T_EMPLOYEE_RESULTS_MONTH_DETAIL"."DetailKey"="T_EMPLOYEE_RESULTS"."DetailKey") INNER JOIN
"LG_SYSTEM"."dbo"."T_PAY_TYPES" "T_PAY_TYPES" ON (("T_EMPLOYEE_RESULTS"."PayType"="T_PAY_TYPES"."PayType") AND ("T_EMPLOYEE_RESULTS"."DataSetID"="T_PAY_TYPES"."DataSetID"))
AND ("T_EMPLOYEE_RESULTS"."FacilityID"="T_PAY_TYPES"."FacilityID")) LEFT OUTER JOIN "LG_SYSTEM"."dbo"."T_JOB_CLASSES" "T_JOB_CLASSES"
ON (("T_EMPLOYEE_RESULTS"."FacilityID"="T_JOB_CLASSES"."FacilityID") AND ("T_EMPLOYEE_RESULTS"."JobClass"="T_JOB_CLASSES"."JobClass")) AND
("T_EMPLOYEE_RESULTS"."DataSetID"="T_JOB_CLASSES"."DataSetID")
)P
UNPIVOT
(MonthHours FOR Mnth IN (Jul, Aug, Sept, Oct, Nov, Dec, Jan, Feb, Mar, Apr, May, Jun)
)AS unpivoted1
UNPIVOT
(MonthDollars FOR Mnth IN (Jul, Aug, Sept, Oct, Nov, Dec, Jan, Feb, Mar, Apr, May, Jun)
)AS unpivoted2