【问题标题】:SQL server pivoting querySQL Server 透视查询
【发布时间】:2013-06-06 18:28:31
【问题描述】:

我有数据集

Ptid  Test Result Date
1     BP    Neg   1/1/2013
1     CG    Pos   1/2/2013

我想将结果格式化为

Ptiid  BP  Date      CG   Date
1      Neg  1/1/2013 Pos  1/2/2013

这在 sql server 旋转中是否可行?

【问题讨论】:

    标签: sql-server database tsql pivot


    【解决方案1】:

    您可以使用带有 CASE 表达式的聚合函数来获取结果:

    select ptid,
      max(case when test = 'BP' then result end) BP,
      max(case when test = 'BP' then date end) BP_Date,
      max(case when test = 'CG' then result end) CG,
      max(case when test = 'CG' then date end) CG_Date
    from yt
    group by ptid;
    

    SQL Fiddle with Demo

    【讨论】:

      猜你喜欢
      • 2012-11-13
      • 1970-01-01
      • 2016-10-28
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多