如何使用sql查询将行转换为列

时间:2016-09-07 11:05:35

标签: sql sql-server ssrs-2008

请参阅以下查询

 select D.emp_id, E.emp_name, A.proj_code, B.proj_name, A.activity_code, C.activity_name,
CONVERT(CHAR(4), A.effort_date, 100) + CONVERT(CHAR(4), A.effort_date, 120) as Month, A.effort_hours
from timesheet_activity A left join
project_master B on A.proj_code = B.proj_code
left join activity_master C on A.activity_code = C.activity_code
left join timesheet D on A.timesheet_id = D.timesheet_id
left join employee_master E on D.emp_id = E.emp_id
WHERE B.proj_name IN ('Sales Support') and (A.effort_date BETWEEN (SELECT CONVERT(VARCHAR(10), CONVERT(date, '30-06-2014', 105), 23))  AND (SELECT CONVERT(VARCHAR(10), CONVERT(date, '02-07-2014', 105), 23)))

结果将是这样的。

this tips

现在我要展示的内容如下所示。

emp_id  emp_name  proj_code  activity_code  activity_name Jun 2014  Jul 2014
101257  Chris      1001       9001           Test          1.5         1

我想把这几个月作为专栏,并显示相同的工作时间。

任何建议都将受到高度赞赏

1 个答案:

答案 0 :(得分:1)

WITH ABC AS ( paste your query to get that result here ) Select * FROM ABC PIVOT (MAX(effort_hours) for Month in ([Jun 2014],[Jul 2014])) as MaxHrs

您可以将函数MAX更改为您想要选择条件的任何数学函数