零件标识符无法绑定

时间:2013-02-24 15:26:04

标签: 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]”。我已经有了一个建议,但仍然有错误。我如何获得排序?

这是我得到的清单:

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 

这是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
  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

This line should do the sort, but i get an error
    ORDER BY    data.[Task deadline]

Msg 4104, Level 16, State 1, Line 62
The multi-part identifier "data.[Leave Production]" could not be bound.
The problem lies in the fact that I need a pivot list that is sorted on the second line of the  group
      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

我已经发送了这个问题,但不幸的是答案不起作用。也许我需要另一个设置。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您的数据子查询似乎有两个字段 - 项目编号和航班日期。如果您想按数据中的任何内容订购,那么这是您唯一的两个选择。

答案 1 :(得分:0)

所以我可能误解了你的要求,但这就是我想出来的:

with M([Project Nr], [Task], [taskdate]) as (
    select '0053', 'Packed', '2013-02-05 17:00:00.000' union
    select '0053 ', 'Leave Production', '2011-04-26 17:00:00.000' union
    select '0053', 'Flight Date', '2011-04-22 17:00:00.000' union
    select '0071', 'Leave Production', '2011-06-24 17:00:00.000' union
    select '0071', 'Packed', '2011-03-09 17:00:00.000' union
    select '0071', 'Flight Date', '2011-07-09 17:00:00.000' union
    select '0081', 'Leave Production', '2013-06-24 17:00:00.000' union
    select '0081', 'Packed', '2011-03-09 17:00:00.000' union
    select '0081', 'Flight Date', '2011-07-09 17:00:00.000' union
    select '0091', 'Leave Production', '2006-06-24 17:00:00.000' union
    select '0091', 'Packed', '2011-03-09 17:00:00.000' union
    select '0091', 'Flight Date', '2011-07-09 17:00:00.000' 
)

select 
    * 
from 
    M AllData
    inner join (
        select 
            ROW_NUMBER() OVER (Order by TaskDate) as ProductionOrder, 
            [Project Nr] 
        from 
            M
        where
            task = 'Leave Production'
    )
ProductionOrder
    on ProductionOrder.[Project Nr] = AllData.[Project Nr]
Order by
    ProductionOrder

它基本上可以完成所有的“离开生产”任务,按顺序为它们分配一个窗口行号,然后将其连接到项目编号上的项目列表,然后按该窗口编号对整个数据集进行排序。

您的一个问题是您正在进行多列数据透视以获取最长截止日期,然后尝试...取消它?所以你最终会击败所有MAX(CASE ..)逻辑的目的。另一个问题是,例如,您的数据查询的结果集为

项目编号,包装,休假,生产日期

但您尝试使用“任务日期和任务”进行取消操作,而源查询中则不存在这些内容。