【问题标题】:Convert single column data to multiple rows [duplicate]将单列数据转换为多行[重复]
【发布时间】:2019-02-17 07:32:47
【问题描述】:

我有一个只有 3 种服务类型的表,我只想旋转服务类型列并显示

ServiceID   FName  LName Servicetpe
1             A      B      ST1
1             A      B      ST2
2             g      e      st1
3             f      h      st1
3             f      h      st2
3             f      h      st3

输出应该是

ServiceID   FName  LName    ST1     ST2     ST31
1             A      B      X        X
2             g      e      X
3             f      h      X        X       X

我试过枢轴没有聚合函数。

【问题讨论】:

    标签: sql sql-server tsql


    【解决方案1】:

    你可以做条件聚合:

    select ServiceID, FName, LName,
           max(case when Servicetpe = 'ST1' then 'X' end) ST1,
           max(case when Servicetpe = 'ST2' then 'X' end) ST2,
           max(case when Servicetpe = 'ST3' then 'X' end) ST3
    from table t
    group by ServiceID, FName, LName;
    

    【讨论】:

      猜你喜欢
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 2016-09-22
      相关资源
      最近更新 更多