按日期分组

时间:2013-02-19 11:09:36

标签: sql sql-server

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

456             pack                  1 april 2013      Pack
456             Leave production      3 april 2013      Leave Production
456             Flight date           9 april 2013      Flight Date

123             pack                  1 april 2013      Pack
123             Leave production      6 april 2013      Leave Production
123             Flight date           9 april 2013      Flight Date

789             pack                  1 april 2013      Pack
789             Leave production      9 april 2013      Leave Production
789             Flight date           9 april 2013      Flight Date

嗨,低于我目前的查询。我怎样才能将它显示在三个行中,按照离开生产的日期排序,它们保持在3组中。如上所述。 p [项目编号必须保持在组中,并且组合在休假生产中进行分类。

    SELECT MSP_EpmProject_UserView.[Project nr]
    ,max(CASE WHEN [Task Type Production] = 'Packed' 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_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]

1 个答案:

答案 0 :(得分:0)

您需要使用UNPIVOT,如下例所示:

SELECT  [Project nr], Task, TaskDate
FROM    (
        SELECT   MSP_EpmProject_UserView.[Project nr]
                ,MAX(CASE   WHEN [Task Type Production] = 'Packed' 
                            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_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]