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

“任务类型生产”是为了确保字段的内容始终一致我只能在数据透视表中创建一列。有没有办法在 3 列中显示这些信息,它看起来像这样:

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'

谢谢安妮

4

2 回答 2

4

这可以使用聚合函数和CASE表达式轻松完成:

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 Fiddle

如果要PIVOT在 SQL Server 中使用该函数,则查询将是:

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

请参阅SQL Fiddle with Demo

最后,这可以使用多个连接到您的表来完成:

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'

请参阅带有演示的 SQL Fiddle

所有查询的结果是:

| PROJECT NR |       PACK | LEAVE PRODUCTION | FLIGHT DATE |
------------------------------------------------------------
|        123 | 2013-04-01 |       2013-04-03 |  2013-04-09 |
于 2013-02-05T16:38:41.243 回答
1

您可以尝试使用相关子查询的传统方法创建交叉表或数据透视表:

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
*/

我已经在具有不错date数据类型的 SQL Server 2008 中完成了此操作,尽管您当然可以使用 stings 或 datetime 来完成此操作。这还假设特定项目编号的每个日期只有一个。

此外,如果可能,请尽量不要在 DB 对象名称中放置空格或其他特殊字符,这些字符会迫使您用括号分隔它们。

于 2013-02-05T16:32:40.040 回答