22,209
社区成员
发帖
与我相关
我的任务
分享
SELECT [apply_id] ,
[1_time] = MAX(CASE WHEN [node] = 1
THEN CONVERT(VARCHAR(16), [time], 120)
ELSE ''
END) ,
[1_operator_name] = MAX(CASE WHEN [node] = 1 THEN [operator_name]
ELSE ''
END) ,
[2_time] = MAX(CASE WHEN [node] = 2
THEN CONVERT(VARCHAR(16), [time], 120)
ELSE ''
END) ,
[2_operator_name] = MAX(CASE WHEN [node] = 2 THEN [operator_name]
ELSE ''
END) ,
[3_time] = MAX(CASE WHEN [node] = 3
THEN CONVERT(VARCHAR(16), [time], 120)
ELSE ''
END) ,
[3_operator_name] = MAX(CASE WHEN [node] = 3 THEN [operator_name]
ELSE ''
END)
FROM ( SELECT * ,
ROW_NUMBER() OVER ( PARTITION BY apply_id, node ORDER BY [time] ) AS Grp
FROM #T
) AS t
GROUP BY [apply_id] ,
Grp;
use Tempdb
go
--> --> 中国风(Roy)生成測試數據
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([node] int,[apply_id] int,[time] Datetime,[operator_name] nvarchar(28))
Insert #T
select 1,10000,'2016/10/28 15:24',N'Clarkson' union all
select 2,10000,'2016/10/28 15:25',N'May' union all
select 3,10000,'2016/10/28 15:26',N'May' union all
select 1,10000,'2016/10/28 15:27',N'Clarkson' union all
select 2,10000,'2016/10/28 15:28',N'May' union all
select 3,10000,'2016/10/28 15:29',N'May' union all
select 1,10000,'2016/10/28 15:30',N'Clarkson' union all
select 2,10000,'2016/10/28 15:31',N'May' union all
select 3,10000,'2016/10/28 15:32',N'May' union all
select 1,10000,'2016/10/28 15:33',N'Clarkson' union all
select 2,10000,'2016/10/28 15:34',N'May'
GO
DECLARE @Sql NVARCHAR(max)=''
Select @Sql=@Sql+',['+RTRIM([node])+'_time]=max(case when [node]='+RTRIM([node])+' then convert(varchar(16),[time],120) else '''' end)'
+',['+RTRIM([node])+'_operator_name]=max(case when [node]='+RTRIM([node])+' then [operator_name] else '''' end)'
from #T GROUP BY [node]
EXEC('SELECT [apply_id]'+@Sql+' FROM (SELECT *,ROW_NUMBER()OVER(PARTITION BY apply_id,node ORDER BY [time]) AS Grp FROM #T) as t group by [apply_id],Grp')
/*
apply_id 1_time 1_operator_name 2_time 2_operator_name 3_time 3_operator_name
10000 2016-10-28 15:24 Clarkson 2016-10-28 15:25 May 2016-10-28 15:26 May
10000 2016-10-28 15:27 Clarkson 2016-10-28 15:28 May 2016-10-28 15:29 May
10000 2016-10-28 15:30 Clarkson 2016-10-28 15:31 May 2016-10-28 15:32 May
10000 2016-10-28 15:33 Clarkson 2016-10-28 15:34 May
*/
use Tempdb
go
--> --> 中国风(Roy)生成測試數據
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([node] int,[apply_id] int,[time] Datetime,[operator_name] nvarchar(28))
Insert #T
select 1,10000,'2016/10/28 15:24',N'Clarkson' union all
select 2,10000,'2016/10/28 15:25',N'May' union all
select 3,10000,'2016/10/28 15:26',N'May' union all
select 1,10000,'2016/10/28 15:27',N'Clarkson' union all
select 2,10000,'2016/10/28 15:28',N'May' union all
select 3,10000,'2016/10/28 15:29',N'May' union all
select 1,10000,'2016/10/28 15:30',N'Clarkson' union all
select 2,10000,'2016/10/28 15:31',N'May' union all
select 3,10000,'2016/10/28 15:32',N'May' union all
select 1,10000,'2016/10/28 15:33',N'Clarkson' union all
select 2,10000,'2016/10/28 15:34',N'May'
GO
DECLARE @Sql NVARCHAR(max)=''
Select @Sql=@Sql+',[time'+RTRIM([node])+']=max(case when [node]='+RTRIM([node])+' then convert(varchar(16),[time],120) else '''' end)'
+',[operator_name'+RTRIM([node])+']=max(case when [node]='+RTRIM([node])+' then [operator_name] else '''' end)'
from #T GROUP BY [node]
EXEC('SELECT [apply_id]'+@Sql+' FROM (SELECT *,ROW_NUMBER()OVER(PARTITION BY apply_id,node ORDER BY [time]) AS Grp FROM #T) as t group by [apply_id],Grp')
/*
apply_id time1 operator_name1 time2 operator_name2 time3 operator_name3
10000 2016-10-28 15:24 Clarkson 2016-10-28 15:25 May 2016-10-28 15:26 May
10000 2016-10-28 15:27 Clarkson 2016-10-28 15:28 May 2016-10-28 15:29 May
10000 2016-10-28 15:30 Clarkson 2016-10-28 15:31 May 2016-10-28 15:32 May
10000 2016-10-28 15:33 Clarkson 2016-10-28 15:34 May
*/