34,590
社区成员
发帖
与我相关
我的任务
分享
--drop table b
create table b([人员Id] int,[卡片号码] varchar(8),[最后修改时间] datetime)
insert b
select 1,'01235','2013-12-12' union all
select 1,'0123456','2013-12-13' union all
select 1,'01234567','2013-12-12' union all
select 2,'02345','2013-12-12' union all
select 2,'023456','2013-12-12'
go
declare @sql nvarchar(max);
set @sql = '';
;with t
as
(
select number as rownum
from master..spt_values
where number >0
and type = 'P'
)
select
@sql = @sql + ',max(case when rownum = '+cast(rownum as varchar)+' then right(''000000''+卡片号码,10) else null end) as 卡片'+
cast(rownum as varchar)
from t
where rownum<= 5 --固定是5个卡号
select @sql = 'select 人员Id' + @sql +
' from (select *,
ROW_NUMBER() over(partition by 人员Id order by @@servername) as rownum
from B)t' +
' group by 人员Id'
--select @sql
exec(@sql)
/*
人员Id 卡片1 卡片2 卡片3 卡片4 卡片5
1 0000001235 0000123456 0001234567 NULL NULL
2 0000002345 0000023456 NULL NULL NULL
*/
--drop table b
create table b([人员Id] int,[卡片号码] varchar(8),[最后修改时间] datetime)
insert b
select 1,'01235','2013-12-12' union all
select 1,'0123456','2013-12-13' union all
select 1,'01234567','2013-12-12' union all
select 2,'02345','2013-12-12' union all
select 2,'023456','2013-12-12'
go
declare @sql nvarchar(max);
set @sql = '';
;with t
as
(
select number as rownum
from master..spt_values
where number >0
and type = 'P'
)
select
@sql = @sql + ',max(case when rownum = '+cast(rownum as varchar)+' then right(''000000''+卡片号码,10) else null end) as 卡片'+
cast(rownum as varchar)
from t
where rownum<= 5 --固定是5个卡号
select @sql = 'select 人员Id' + @sql +
' from (select *,
ROW_NUMBER() over(partition by 人员Id order by 最后修改时间) as rownum
from B)t' +
' group by 人员Id'
--select @sql
exec(@sql)
/*
人员Id 卡片1 卡片2 卡片3 卡片4 卡片5
1 0000001235 0001234567 0000123456 NULL NULL
2 0000002345 0000023456 NULL NULL NULL
*/
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-12-16 14:44:05
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([人员Id] int,[卡片号码] varchar(8),[最后修改时间] datetime)
insert [B]
select 1,'01235','2013-12-12' union all
select 1,'0123456','2013-12-13' union all
select 1,'01234567','2013-12-12' union all
select 2,'02345','2013-12-12' union all
select 2,'023456','2013-12-12'
--------------开始查询--------------------------
DECLARE @date DATE
DECLARE @id INT
SET @id=5
SET @date='2013-12-12'
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename('卡片'+CONVERT(VARCHAR(2),ROW_NUMBER()OVER(ORDER BY GETDATE())))+'=max(case when id='+quotename(id,'''')+' then [卡片号码] else NULL end)'
from (SELECT right('0000000000'+[卡片号码],10) [卡片号码],ROW_NUMBER()OVER(PARTITION BY [人员Id] ORDER BY [人员Id]) id FROM [B] WHERE [最后修改时间]<=@date ) B
WHERE id<=@id
group by id
exec('select [人员Id]'+@s+' from (SELECT [人员Id],right(''0000000000''+[卡片号码],10) [卡片号码],ROW_NUMBER()OVER(PARTITION BY [人员Id] ORDER BY [人员Id]) id FROM [B]) B group by [人员Id]')
/*
人员Id 卡片1 卡片2
----------- ------------------ ------------------
1 0000001235 0000123456
2 0000002345 0000023456
*/