27,579
社区成员
发帖
与我相关
我的任务
分享
SELECT
[LinkCode],[1],[2],[3],[4],[LinkTime],[IsComplete]
FROM (SELECT [LinkCode],[BarCode],[LinkTime]=MAX([LinkTime])OVER(PARTITION BY [LinkCode],[IsComplete]),[CodeType],[IsComplete] FROM #T ) AS a
PIVOT(MAX([BarCode]) FOR [CodeType] IN([1],[2],[3],[4])) AS b
create table 已知表
(ID int,
LinkCode varchar(10),
BarCode varchar(10),
LinkTime datetime,
CodeType int,
IsComplete int)
insert into 已知表
select 1, '111', '111', getdate() , 1 , 0 union all
select 2, '111', '222', getdate() , 2 , 0 union all
select 3, '111', '333', getdate() , 3 , 0 union all
select 4, 'aaa', 'aaa', getdate() , 1 , 1 union all
select 5, 'aaa', 'bbb', getdate() , 2 , 1 union all
select 6, 'aaa', 'ccc', getdate() , 3 , 1 union all
select 7, 'aaa', 'ddd', getdate() , 4 , 1
declare @tsql varchar(6000)
select @tsql=isnull(@tsql+',','')
+'max(case when CodeType='+rtrim(number)+' then BarCode else null end) ['+rtrim(number)+'] '
from master.dbo.spt_values
where type='P' and number>=1
and number<=(select top 1 count(1) from 已知表 group by LinkCode order by count(1) desc)
select @tsql='select ID=row_number() over(order by getdate()),'+@tsql+','
+' LinkTime=max(LinkTime),
IsComplete=case when exists(select 1 from 已知表 u where u.LinkCode=t.LinkCode and u.IsComplete=0) then 0 else 1 end
from 已知表 t
group by t.LinkCode '
exec(@tsql)
/*
ID 1 2 3 4 LinkTime IsComplete
-------------------- ---------- ---------- ---------- ---------- ----------------------- -----------
1 111 222 333 NULL 2016-05-30 09:46:40.547 0
2 aaa bbb ccc ddd 2016-05-30 09:46:40.547 1
(2 row(s) affected)
*/
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[LinkCode] nvarchar(23),[BarCode] nvarchar(23),[LinkTime] DATETIME,[CodeType] int,[IsComplete] int)
Insert #T
select 1,N'111',N'111',getdate(),1,0 union all
select 2,N'111',N'222',getdate(),2,0 union all
select 3,N'111',N'333',getdate(),3,0 union all
select 4,N'aaa',N'aaa',getdate(),1,1 union all
select 5,N'aaa',N'bbb',getdate(),2,1 union all
select 6,N'aaa',N'ccc',getdate(),3,1 union all
select 7,N'aaa',N'ddd',getdate(),4,1
GO
SELECT
[LinkCode],[1],[2],[3],[4],[LinkTime],[IsComplete]
FROM (SELECT [LinkCode],[BarCode],[LinkTime],[CodeType],[IsComplete] FROM #T ) AS a
PIVOT(MAX([BarCode]) FOR [CodeType] IN([1],[2],[3],[4])) AS b
/*
LinkCode 1 2 3 4 LinkTime IsComplete
111 111 222 333 NULL 2016-05-30 09:56:06.653 0
aaa aaa bbb ccc ddd 2016-05-30 09:56:06.653 1
*/
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[LinkCode] nvarchar(23),[BarCode] nvarchar(23),[LinkTime] DATETIME,[CodeType] int,[IsComplete] int)
Insert #T
select 1,N'111',N'111',getdate(),1,0 union all
select 2,N'111',N'222',getdate(),2,0 union all
select 3,N'111',N'333',getdate(),3,0 union all
select 4,N'aaa',N'aaa',getdate(),1,1 union all
select 5,N'aaa',N'bbb',getdate(),2,1 union all
select 6,N'aaa',N'ccc',getdate(),3,1 union all
select 7,N'aaa',N'ddd',getdate(),4,1
GO
DECLARE @Sql NVARCHAR(max)=''
Select @Sql=@Sql+','+QUOTENAME([CodeType])+'=max(CASE WHEN [CodeType]='+QUOTENAME([CodeType],'''')+' THEN BarCode else '''' END)' FROM #T GROUP BY [CodeType]
EXEC('SELECT [LinkCode]'+@Sql+',[LinkTime],[IsComplete] FROM #T GROUP BY [LinkCode],[LinkTime],[IsComplete]')
/*
LinkCode 1 2 3 4 LinkTime IsComplete
111 111 222 333 2016-05-30 09:51:52.373 0
aaa aaa bbb ccc ddd 2016-05-30 09:51:52.373 1
*/
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[LinkCode] nvarchar(23),[BarCode] nvarchar(23),[LinkTime] DATETIME,[CodeType] int,[IsComplete] int)
Insert #T
select 1,N'111',N'111',getdate(),1,0 ;INSERT #T
select 2,N'111',N'222',getdate(),2,0 ;INSERT #T
select 3,N'111',N'333',getdate(),3,0 ;INSERT #T
select 4,N'aaa',N'aaa',getdate(),1,1 ;INSERT #T
select 5,N'aaa',N'bbb',getdate(),2,1 ;INSERT #T
select 6,N'aaa',N'ccc',getdate(),3,1 ;INSERT #T
select 7,N'aaa',N'ddd',getdate(),4,1
GO
SELECT
[LinkCode],[1],[2],[3],[4],[LinkTime],[IsComplete]
FROM (SELECT [LinkCode],[BarCode],[LinkTime]=MAX([LinkTime])OVER(PARTITION BY [LinkCode],[IsComplete]),[CodeType],[IsComplete] FROM #T ) AS a
PIVOT(MAX([BarCode]) FOR [CodeType] IN([1],[2],[3],[4])) AS b
/*
LinkCode 1 2 3 4 LinkTime IsComplete
111 111 222 333 2016-05-30 09:51:52.373 0
aaa aaa bbb ccc ddd 2016-05-30 09:51:52.373 1
*/