1
"Project nr"    "Task"                "Task deadline"  "Task Type Production"

456             packed                1 april 2013      Packed
456             Leave production      3 april 2013      Leave Production
456             Flight date           9 april 2013      Flight Date
123             packed                1 april 2013      Packed
123             Leave production      6 april 2013      Leave Production
123             Flight date           9 april 2013      Flight Date
789             packed                1 april 2013      Packed
789             Leave production      9 april 2013      Leave Production
789             Flight date           9 april 2013      Flight Date

我需要一个像上面这样的列表 我想按“停止生产”的日期排序项目 nr 必须像上面的列表一样保持在 3 组内。

我已经有了这个 SQL。该列表有效,但排序无效。错误是:无法绑定多部分标识符“任务。[任务期限]”。我如何得到排序?

    SELECT  [Project nr], Task, TaskDate
    FROM    (
    SELECT   MSP_EpmProject_UserView.[Project nr]
            ,MAX(CASE   WHEN [Task Type Production] = 'Packed' 
                        THEN [Task deadline] 
                    END) AS Packed
            ,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_EpmProject_UserView 
            LEFT JOIN MSP_EpmTask_UserView 
                ON MSP_EpmProject_UserView.ProjectUID = MSP_EpmTask_UserView.ProjectUID 
            LEFT JOIN MSP_EpmAssignment_UserView 
                ON MSP_EpmTask_UserView.TaskUID = MSP_EpmAssignment_UserView.TaskUID 
            LEFT JOIN MSP_EpmResource_UserView 
                ON MSP_EpmAssignment_UserView.ResourceUID = MSP_EpmResource_UserView.ResourceUID 
    WHERE (MSP_EpmTask_UserView.TaskIsProjectSummary = 0) 
    GROUP BY MSP_EpmProject_UserView.[Project nr]
    ) data
    UNPIVOT
    (
      TaskDate
      for [Task] in ([Pack],[Leave Production],[Flight Date])
    ) piv
    ORDER BY    data.[Task deadline]

这是 ORDER BY 注释掉的结果。我也想要一个没有日期的字段的结果。

Project Nr     Task             TaskDate
0053           Packed           2013-02-05 17:00:00.000
0053           Leave Production 2011-04-26 17:00:00.000
0053           Flight Date      2011-04-22 17:00:00.000
0071           Leave Production 2011-06-24 17:00:00.000
0081           Leave Production 2012-03-09 17:00:00.000
0081-01        Leave Production 2012-03-09 17:00:00.000 
4

1 回答 1

1

我假设您有一个看起来像您问题中的初始表的表/视图/CTE。使用窗口函数获取所有相同“Project nr”的值,然后进行排序:

select "Project nr", "Task", "Task deadline", "Task Type Production"
from (select t.*,
             MAX(case when task = 'Leave Production' then "Task deadline" end) over (partition by "Project nr") as lpdl
      from t
     ) t
order by lpdl, 1
于 2013-02-19T21:41:00.173 回答