--未旋转之前的查询结果 select s.Name ShiftName,h.BusinessEntityID,d.Name as DpartmentName from HumanResources.EmployeeDepartmentHistory h inner join HumanResources.Department d on h.DepartmentID=d.DepartmentID inner join HumanResources.Shift s on s.ShiftID=h.ShiftID WHERE ENDDATE IS NULL AND d.Name IN('Production','Engineering','Marketing') order by ShiftName
![](https://i-blog.csdnimg.cn/blog_migrate/1df70c40f8afaabe9e1da1ded79824e0.png)
--旋转之后的结果 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.DepartmentID inner join HumanResources.Shift s on s.ShiftID=h.ShiftID WHERE ENDDATE IS NULL AND d.Name IN('Production','Engineering','Marketing') )t pivot (count(BusinessEntityID) for DpartmentName in ([Production],[Engineering],[Marketing]))b order by ShiftName
![](https://i-blog.csdnimg.cn/blog_migrate/85d42f68a3d2073ca7ac25c80139da49.png)