具有多部分标识符错误的日期排序列表

时间:2013-02-19 21:34:44

标签: sql

"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。该列表正在运行,但排序无效。错误是:无法绑定多部分标识符“task。[task deadline]”。 我如何获得排序?

    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 

1 个答案:

答案 0 :(得分: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