我有下面的sql表tbl_deptExpenses,我想将部门填充为Column:
表结构:
DeptId Department Expenses
1 IT 1000
2 Accounting 2000
3 Catering 400
我要实现的是查询以返回下表-部门值已更改为“字段”:
IT Accounting Catering
1000 2000 400
我尝试了以下方法,但似乎不起作用:
SELECT [d.department] = d.department,
[Expenses] = d.expenses
from tbl_deptExpenses d
答案 0 :(得分:0)
您可以尝试对整个表进行数据透视查询:
SELECT
MAX(CASE WHEN Department = 'IT' THEN Expenses END) AS IT,
MAX(CASE WHEN Department = 'Accounting' THEN Expenses END) AS Accounting,
MAX(CASE WHEN Department = 'Catering' THEN Expenses END) AS Catering
FROM tbl_deptExpenses;