我有下面的查詢,它運行良好,但它的靜態數據透視表。我想動態創建它。我想知道這段代碼需要什麼修改,以便我可以把它變成動態的關鍵。 提前致謝!需要在sql server中創建動態數據透視表
select * from
(
select g.NationalityName_1,DepartmentName_1,h.EmployeeID from HR_Department hr
join
HR_EmploymentInformation h
on h.DepartmentID=hr.DepartmentID
join
HR_EmployeeDetail e
on e.EmployeeID=h.EmployeeID
join
GEN_Nationality g
on g.NationalityID=e.DefaultNationalityID
) as fev
pivot
(
count(employeeId)
for departmentname_1 in (Administration,Academia,[New Department 1],[New Department 2],[New Department 3],[New Department 5])
) as pvt
這是輸出:
NationalityName_1 Administration Academia New Department 1 New Department 2 New Department 3 New Department 5
Algeria 0 1 0 0 0 0
這是上面的查詢和正確的輸出,我面臨的問題是,如果我創建一個新的部門我不得不再次改變一個我不想要的代碼。
編寫動態
PIVOT
如果我爲你提供動態的解決方案則可以應用在你的代碼?因爲沒有樣本數據和預期的產出,很難處理它。 – Susang是的,我可以運行查詢.. –