将SQl表行转换为SQL Server 2005中的列 [英] Convert SQl table rows as column in SQL server 2005
问题描述
您好,以下是我的表数据:
EmployeeDetailID名称缺席日月
3 Vishal Patel 5月1日13日
3 Vishal Patel 3月3日13日
3 Vishal Patel 7月13日
现在我希望这些数据转换如下:
EmployeeDetailID名称May-13 Jun-13 Jul-13
3 Vishal Patel 1 3 4
请提前帮助我查询和thnx
Hello , following is my table data :
EmployeeDetailID Name AbsentDays Month
3 Vishal Patel 1 May-13
3 Vishal Patel 3 Jun-13
3 Vishal Patel 4 Jul-13
Now i want this data to be converted like below :
EmployeeDetailID Name May-13 Jun-13 Jul-13
3 Vishal Patel 1 3 4
Please help me out with the query and thnx in advance
推荐答案
使用PIVOT
试试自己
http://blog.sqlauthority.com/20 08/06/07 / sql-server-pivot-and-unpivot-table-examples / [ ^ ]
Use PIVOT
Try yourself
http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/[^]
你可以在sql中进行旋转..
点击以下链接获取更多信息。
在SQL查询中使用数据透视的简单方法 [ ^ ]
看起来和你的要求一样。
You can do pivoting in sql..
follow the link below for more info.
Simple Way To Use Pivot In SQL Query[^]
It seems like same as your requirement.
试试这个:
Try this:
SELECT EmployeeDetailID, name, [May-13],[Jun-13],[Jul-13]
FROM
(SELECT EmployeeDetailID, name, AbsentDays, month FROM table1) AS src
PIVOT
(
sum(AbsentDays) FOR month IN ([May-13],[Jun-13],[Jul-13])
)AS pvt
这篇关于将SQl表行转换为SQL Server 2005中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!