我需要将第一个查询的数据用于第二个查询以获得正确的结果,但我无法完成。谁能帮我解决这个问题。
SELECT [Project Nr], Task, TaskDate
FROM (
SELECT MSP_EpmProject_UserView.[Project Nr]
,MAX(CASE WHEN [Task Type tempress] = 'Packed'
THEN [Taskdeadline]
END) AS Pack
,MAX(CASE WHEN [Task Type tempress] = 'Leave Production'
THEN [Taskdeadline]
END) AS [Leave Production]
,MAX(CASE WHEN [Task Type tempress] = 'Flight Date'
THEN [Taskdeadline]
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 Vaassen],[Flight Date])
) piv
这部分查询必须使用上面查询的数据:
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