代码之家  ›  专栏  ›  技术社区  ›  Sandeep Thomas

基于表中两列的透视

  •  0
  • Sandeep Thomas  · 技术社区  · 6 年前

    我有一张这样的桌子

    PCode      Milestone      DeliveryDate       Status
    ------------------------------------------------------
    P1234        Start             14/5/2019     Complete
    P1234        End               17/6/2019     Complete
    P2345        Start              8/6/2019      Progress
    P2345        End                19/6/2019     Progress
    P7335        Start              18/8/2019     Provisional
    P7335        End                19/9/2019     Provisional
    -----------------------------------------------------------
    

    根据以上数据,我需要得到一个这样的表格

    PCode     Start          End        Status
    ---------------------------------------------
    P1234     14/05/2019    17/06/2019   Completed
    P2345     8/6/2019      19/06/2019   Progress
    P7335     18/08/2019   19/09/2019    Provisional
    ----------------------------------------------
    

    0 回复  |  直到 6 年前
        1
  •  2
  •   John Cappelletti    6 年前

    一个简单的条件聚合应该可以做到这一点

    [Status] 是一致的 PCode

    Selet PCode
         ,[Start]  = min(case when Milestone='Start' then DeliveryDate end)
         ,[Ebd]    = max(case when Milestone='End'   then DeliveryDate end)
         ,[Status] = max([Status])
     From  YourTable
     Group By PCode
    

    编辑-查找开始和结束状态

    Select PCode
         ,[Start]       = min(case when Milestone='Start' then DeliveryDate end)
         ,[End]         = max(case when Milestone='End'   then DeliveryDate end)
         ,[StatusStart] = min(case when Milestone='Start' then [Status] end)
         ,[StatusEnd]   = min(case when Milestone='End'   then [Status] end)
     From   YourTable
     Group By PCode