2017-08-30 46 views
0

我要轉和組一些列來自:SQL移調和組一些列

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 

有什麼建議?

+0

我使用的MS SQL Server – Spjcc

回答

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 
0

您可以使用此代碼塊,旋轉:

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