【问题标题】:SQL Transpose and group some columnsSQL转置和分组一些列
【发布时间】:2017-08-30 08:54:36
【问题描述】:

我想从以下位置转置和分组一些列:

Agent     Date       Interval     EmailTime     PhoneTime
John      1-1-2017    00:00        00:15:00      NULL
John      1-1-2017    00:15        00:10:00      00:05:00
John      1-1-2017    00:30        NULL          00:15:00

收件人:

Agent     Date       Interval      State          Duration
John      1-1-2017    00:00        EmailTime      00:15:00
John      1-1-2017    00:15        EmailTime      00:10:00
John      1-1-2017    00:15        PhoneTime      00:05:00
John      1-1-2017    00:30        PhoneTime      00:05:00

有什么建议吗?

【问题讨论】:

  • 我正在使用 MS SQL Server

标签: sql sql-server pivot transpose


【解决方案1】:

你不需要一个支点。它可以通过联合所有查询来实现。

Select Agent, Date, Interval    'EmailTime' State,  EmailTime     from table where EmailTime is not null 
union all 
Select Agent, Date, Interval    'PhoneTime' State,  PhoneTime     from table where PhoneTime is not null 

【讨论】:

    【解决方案2】:

    您可以使用此代码 blcok:

    select agent ,date ,interval 'EmailTime' as state ,emailtime as duration from table where emailtime is not null union all select agent ,date ,interval 'PhoneTime' as state ,phonetime as duration from table where phonetime is not null union all select agent ,date ,interval 'NULL' state ,phonetime as duration from table where emailtime is null and phonetime is null

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 2018-08-12
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      相关资源
      最近更新 更多