Simple Pivot sample

前端 未结 1 548
星月不相逢
星月不相逢 2020-12-21 13:31

I need the a report of all masterid\'s but it may only be one on a row.. I know that\'s a simple thing to do but I can\'t figure out the syntax correctly.

I attached

相关标签:
1条回答
  • 2020-12-21 14:04
    SELECT MasterID, 
      [Basic Phone] = MAX([Basic Phone]),
      [Pixi] = MAX([Pixi]),
      [Blackberry] = MAX([Blackberry])
    FROM
    (
      SELECT MasterID, [Basic Phone],[Pixi],[Blackberry]
      FROM dbo.Services AS s
      PIVOT 
      (
        MAX([Status]) FOR [Type] IN ([Basic Phone],[Blackberry],[Pixi])
      ) AS p
    ) AS x
    GROUP BY MasterID;
    

    Or more simply - and credit to @YS. for pointing out my redundancy.

    SELECT MasterID, 
      [Basic Phone],
      [Pixi],
      [Blackberry]
    FROM
    (
      SELECT MasterID, Status, Type FROM dbo.Services
    )
    AS s
    PIVOT 
    (
      MAX([Status]) FOR [Type] IN ([Basic Phone], [Blackberry], [Pixi])
    ) AS p;
    
    0 讨论(0)
提交回复
热议问题