Sql server 使用一条sql语句在数据透视表中创建多个列
我需要在数据透视表中对相同项目编号的项目数据按日期排序 该项目如下所示:Sql server 使用一条sql语句在数据透视表中创建多个列,sql-server,pivot,Sql Server,Pivot,我需要在数据透视表中对相同项目编号的项目数据按日期排序 该项目如下所示: "Project nr" "Task" "Task deadline" "Task Type Production" 123 pack 1 april 2013 Pack 123 Leave production 3 april 2013 Leave Production 123
"Project nr" "Task" "Task deadline" "Task Type Production"
123 pack 1 april 2013 Pack
123 Leave production 3 april 2013 Leave Production
123 Flight date 9 april 2013 Flight Date
Project nr ; Pack ; leave production ; flightdate
SELECT [TaskDeadline] AS Packed
FROM MSP_EpmTask_UserView where [Task Type Production] = 'Packed'
SELECT [TaskDeadline] AS LeaveProduction
FROM MSP_EpmTask_UserView where [Task Type Production] = 'Leave Production'
SELECT [TaskDeadline] AS FlightDate
FROM MSP_EpmTask_UserView where [Task Type Production] = 'Flight Date'
“任务型生产”是为了确保字段内容始终一致
我只能在透视表中创建一列。有没有办法在3列中显示这些信息
它将如下所示:
"Project nr" "Task" "Task deadline" "Task Type Production"
123 pack 1 april 2013 Pack
123 Leave production 3 april 2013 Leave Production
123 Flight date 9 april 2013 Flight Date
Project nr ; Pack ; leave production ; flightdate
SELECT [TaskDeadline] AS Packed
FROM MSP_EpmTask_UserView where [Task Type Production] = 'Packed'
SELECT [TaskDeadline] AS LeaveProduction
FROM MSP_EpmTask_UserView where [Task Type Production] = 'Leave Production'
SELECT [TaskDeadline] AS FlightDate
FROM MSP_EpmTask_UserView where [Task Type Production] = 'Flight Date'
谢谢
Anne您可以尝试使用相关子查询的传统方法创建交叉选项卡或轴:
select [Project nr]
, (select [Task deadline] from MSP_EpmTask_UserView where [Project nr] = t.[Project nr] and [Task Type Production] = 'Pack') as Pack
, (select [Task deadline] from MSP_EpmTask_UserView where [Project nr] = t.[Project nr] and [Task Type Production] = 'Leave Production') as [Leave Production]
, (select [Task deadline] from MSP_EpmTask_UserView where [Project nr] = t.[Project nr] and [Task Type Production] = 'Flight Date') as [Flight Date]
from MSP_EpmTask_UserView t
group by [Project nr]
order by [Project nr]
/*
Project nr Pack Leave Production Flight Date
----------- ---------- ---------------- -----------
123 2013-04-01 2013-04-03 2013-04-09
*/
我已经在SQLServer2008中完成了这项工作,它具有漂亮的date
数据类型,尽管您当然可以使用stings或datetime来完成这项工作。这还假设特定项目的每个日期只有一个
此外,如果可能,请尽量不要在DB对象名称中添加空格或其他特殊字符,因为这些字符会迫使您用括号对其进行分隔。这可以通过使用聚合函数和大小写表达式轻松完成:
select [Project nr],
MAX(case when [Task Type Production] = 'Pack' then [Task deadline] end) as Pack,
MAX(case when [Task Type Production] = 'Leave Production' then [Task deadline] end) as [Leave Production],
MAX(case when [Task Type Production] = 'Flight Date' then [Task deadline] end) as [Flight Date]
from MSP_EpmTask_UserView
group by [Project nr]
看
如果要在SQL Server中使用PIVOT
函数,则查询将为:
select *
from
(
select [Project nr],[Task deadline], [Task Type Production]
from MSP_EpmTask_UserView
) src
pivot
(
max([Task deadline])
for [Task Type Production] in ([Pack], [Leave Production],
[Flight Date])
) piv
看
最后,可以使用到表的多个联接来完成此操作:
select t1.[Project nr],
t1.[Task deadline] pack,
t2.[Task deadline] [Leave Production],
t3.[Task deadline] [Flight Date]
from MSP_EpmTask_UserView t1
left join MSP_EpmTask_UserView t2
on t1.[Project nr] = t2.[Project nr]
and t2.[Task Type Production] = 'Leave Production'
left join MSP_EpmTask_UserView t3
on t1.[Project nr] = t3.[Project nr]
and t3.[Task Type Production] = 'Flight Date'
where t1.[Task Type Production] = 'Pack'
看
所有查询的结果为:
| PROJECT NR | PACK | LEAVE PRODUCTION | FLIGHT DATE |
------------------------------------------------------------
| 123 | 2013-04-01 | 2013-04-03 | 2013-04-09 |
+1为选项。我喜欢
case
版本,尽管pivot
语法在我的方框中同样快速。@TimLehner我喜欢提供查询的其他版本。我更喜欢pivot,但有些人很难理解它的语法。