Cross Tab - Storing different dates (Meeting1, Meeting2, Meeting 3 etc) in the same column

后端 未结 4 2082
夕颜
夕颜 2021-01-06 10:49

I need to keep track of different dates (dynamic). So for a specific Task you could have X number of dates to track (for example DDR1 meeting date, DDR2 meeting date, Due Da

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-06 11:19

    I don't have personal experience with the pivot operator, it may provide a better solution.

    But I've used a case statement in the past

    SELECT 
        TaskDescription, 
        CASE(DateTypeID = 1, Tasks_DateType.Date) AS DDr1, 
        CASE(DateTypeID = 2, Tasks_DateType.Date) AS DDr2,
        ...
    FROM Tasks 
        INNER JOIN Tasks_DateType  ON Tasks.ID = Tasks_DateType.TasksID
        INNER JOIN DateType ON Tasks_DateType.DateTypeID = DateType.DateTypeID
    GROUP BY TaskDescription
    

    This will work, but will require you to change the SQL whenever there are more Task descriptions added, so it's not ideal.

    EDIT:

    It appears as though the PIVOT keyword was added in SqlServer 2005, this example shows how to do a pivot query in both 2000 & 2005, but it is similar to my answer.

提交回复
热议问题