--未旋转之前的查询结果 select s.Name ShiftName,h.BusinessEntityID,d.Name as DpartmentName from HumanResources.EmployeeDepartmentHistory h inner join HumanResources.Department d on h.DepartmentID=d.DepartmentIDinner join HumanResources.Shift s on s.ShiftID=h.ShiftIDWHERE ENDDATE IS NULL ANDd.Name IN('Production','Engineering','Marketing')order by ShiftName
--旋转之后的结果select ShiftName,Production,Engineering,Marketing from (select s.Name ShiftName,h.BusinessEntityID,d.Name as DpartmentName from HumanResources.EmployeeDepartmentHistory h inner join HumanResources.Department d on h.DepartmentID=d.DepartmentIDinner join HumanResources.Shift s on s.ShiftID=h.ShiftIDWHERE ENDDATE IS NULL ANDd.Name IN('Production','Engineering','Marketing'))t pivot (count(BusinessEntityID)for DpartmentName in ([Production],[Engineering],[Marketing]))border by ShiftName