22,210
社区成员
发帖
与我相关
我的任务
分享
create table tb (编号 varchar(10),日期 varchar(10),日期时间 datetime)
insert into tb values('001', '2008-02-02', '2008-02-02 20:00:00')
insert into tb values('001', '2008-02-02', '2008-02-02 21:00:00')
insert into tb values('001', '2008-02-02', '2008-02-02 23:00:00')
go
--静态SQL,指一个编号最多有3个。
select 编号 ,
max(case px when 1 then right(convert(varchar(16),日期时间,120),5) else '' end) 时间1,
max(case px when 2 then right(convert(varchar(16),日期时间,120),5) else '' end) 时间2,
max(case px when 3 then right(convert(varchar(16),日期时间,120),5) else '' end) 时间3
from
(
select * , px = (select count(1) from tb where 编号=t.编号 and 日期=t.日期 and 日期时间<t.日期时间) + 1 from tb t
) m
group by 编号
/*
编号 时间1 时间2 时间3
---------- ---------- ---------- ----------
001 20:00 21:00 23:00
*/
--动态SQL,指一个编号时间个数不定。
declare @sql varchar(8000)
set @sql = 'select 编号'
select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then right(convert(varchar(16),日期时间,120),5) else '' '' end) [时间' + cast(px as varchar) + ']'
from (select distinct px from (select * , px = (select count(1) from tb where 编号=t.编号 and 日期=t.日期 and 日期时间<t.日期时间) + 1 from tb t) m) as a
set @sql = @sql + ' from (select * , px = (select count(1) from tb where 编号=t.编号 and 日期=t.日期 and 日期时间<t.日期时间) + 1 from tb t) m group by 编号'
exec(@sql)
/*
编号 时间1 时间2 时间3
---------- ---------- ---------- ----------
001 20:00 21:00 23:00
*/
drop table tb
create table tb(编号 varchar(5),日期 varchar(10),日期时间 datetime)
insert into tb
select '001','2008-02-02','2008-02-02 10:00:00' union all
select '001','2008-02-02','2008-02-02 20:00:00' union all
select '001','2008-02-02','2008-02-02 21:00:00' union all
select '001','2008-02-02','2008-02-02 23:00:00' union all
select '001','2008-02-03','2008-02-03 10:00:00' union all
select '001','2008-02-03','2008-02-03 20:00:00' union all
select '001','2008-02-03','2008-02-03 21:00:00' union all
select '001','2008-02-02','2008-02-02 23:00:00' union all
select '002','2008-02-02','2008-02-02 08:00:00' union all
select '002','2008-02-02','2008-02-02 12:00:00' union all
select '002','2008-02-02','2008-02-02 13:00:00'
go
declare @s nvarchar(4000),@i int
--用distinct去掉有重复的记录
set @s = ''
select @s = @s + ',max(case when con = '+ ltrim(con) +' then convert(varchar(10),日期时间,108) else null end) as 时间'+ltrim(con+1)
from (select distinct con=(select count(distinct 日期时间)
from tb where 编号=a.编号 and 日期=a.日期 and 日期时间 < a.日期时间) from tb a ) a
set @s='select 编号,日期'+@s+
' from (select *,con=(select count(distinct 日期时间) --distinct去掉有重复的记录
from tb where 编号=a.编号 and 日期=a.日期 and 日期时间 < a.日期时间) from tb a ) tmp group by 编号,日期'
--print @s --显示语句
exec (@s)
/*
编号 日期 时间1 时间2 时间3 时间4
----- ---------- ---------- ---------- ---------- ----------
001 2008-02-02 10:00:00 20:00:00 21:00:00 23:00:00
002 2008-02-02 08:00:00 12:00:00 13:00:00 NULL
001 2008-02-03 10:00:00 20:00:00 21:00:00 NULL
警告: 聚合或其它 SET 操作消除了空值。
*/
drop table tb
create table tb(编号 varchar(5),日期 varchar(10),日期时间 datetime)
insert into tb
select '001','2008-02-02','2008-02-02 10:00:00' union all
select '001','2008-02-02','2008-02-02 20:00:00' union all
select '001','2008-02-02','2008-02-02 21:00:00' union all
select '001','2008-02-02','2008-02-02 23:00:00' union all ---
select '001','2008-02-03','2008-02-03 10:00:00' union all
select '001','2008-02-03','2008-02-03 20:00:00' union all
select '001','2008-02-03','2008-02-03 21:00:00' union all
select '001','2008-02-02','2008-02-02 23:00:00' union all --这条记录重复
select '002','2008-02-02','2008-02-02 08:00:00' union all
select '002','2008-02-02','2008-02-02 12:00:00' union all
select '002','2008-02-02','2008-02-02 13:00:00'
go
declare @s nvarchar(4000),@i int
--用distinct去掉有重复的记录
select top 1 @s='',@i=count(distinct 日期时间) from tb group by 编号,日期 order by count(distinct 日期时间) desc--最同一天同一编号最多的记录数
while @i>0
select @s=',[时间'+rtrim(@i)+']=max(case when con='+rtrim(@i)+' then convert(varchar(5),日期时间,14) else '''' end)'+@s,
@i=@i-1
set @s='select 编号,日期'+@s+
' from (select *,con=(select count(distinct 日期时间) --distinct去掉有重复的记录
from tb where 编号=a.编号 and 日期=a.日期 and 日期时间!>a.日期时间) from tb a )tmp group by 编号,日期'
--print @s --显示语句
exec (@s)
go
/*
号 日期 时间1 时间2 时间3 时间4
----- ---------- ----- ----- ----- -----
001 2008-02-02 10:00 20:00 21:00 23:00
002 2008-02-02 08:00 12:00 13:00
001 2008-02-03 10:00 20:00 21:00
*/
Create table t(Id varchar(10),Createtime varchar(10),date datetime )
insert into t select '001','2008-02-02','2008-02-02 20:00:00'
insert into t select '001','2008-02-02','2008-02-02 21:00:00'
insert into t select '001','2008-02-02','2008-02-02 23:00:00'
declare @sql varchar(8000)
set @sql='select Id,createtime'
select @sql=@sql+',max(case when date=cast('''+ltrim(date)+''' as varchar(30)) then convert(varchar(10),date,108) else null end ) as 时间'+ltrim(num)
from (select distinct date,(select count(1) from t b where a.id=b.id and a.createtime=b.createtime and b.date<=a.date ) as num from t a) a
set @sql=@sql+' from t group by id,createtime'
exec(@sql)
/*
Id createtime 时间1 时间2 时间3
---------- ---------- ---------- ---------- ----------
001 2008-02-02 20:00:00 21:00:00 23:00:00
*/
if object_id('tb') is not null
drop table tb
go
create table tb(编号 varchar(5),日期 varchar(10),日期时间 datetime)
insert into tb
select '001','2008-02-02','2008-02-02 10:00:00' union all
select '001','2008-02-02','2008-02-02 20:00:00' union all
select '001','2008-02-02','2008-02-02 21:00:00' union all
select '001','2008-02-02','2008-02-02 23:00:00' union all
select '002','2008-02-02','2008-02-02 08:00:00' union all
select '002','2008-02-02','2008-02-02 12:00:00' union all
select '002','2008-02-02','2008-02-02 13:00:00'
go
declare @i int,@max int,@s nvarchar(2000)
select @i=1,@s='',@max=(select max(num) from (select count(1) as num from tb group by 编号) dd)
while @i<=@max
select @s=@s+',[时间'+rtrim(@i)+']=max(case when con='+rtrim(@i)+' then convert(char(5),日期时间,108) else '''' end)',@i=@i+1
set @s='select 编号,日期'+@s+' from (select *,con=(select count(1) from tb where 编号=t.编号 and 日期=t.日期 and 日期时间<=t.日期时间) from tb t)tmp group by 编号,日期'
exec(@s)
/*
编号 日期 时间1 时间2 时间3 时间4
--------------------------------------------------------
001 2008-02-02 10:00 20:00 21:00 23:00
002 2008-02-02 08:00 12:00 13:00
*/
if object_id('tb') is not null
drop table tb
create table tb(编号 varchar(5),日期 varchar(10),日期时间 datetime)
insert into tb
select '001','2008-02-02','2008-02-02 20:00:00' union all
select '001','2008-02-02','2008-02-02 21:00:00' union all
select '001','2008-02-02','2008-02-02 23:00:00'
declare @exec varchar(8000),@i int
select @exec='',@i=1
select @exec=@exec+',[时间'+ltrim(cast(@i as char(2)))+']=max(case when 日期时间= '''+convert(varchar(20),日期时间)+''' then convert(char(5),日期时间,108) else '''' end)',@i=@i+1 from tb group by 日期时间
set @exec='select 编号, 日期'+@exec+' from tb group by 编号,日期 order by 编号 '
exec (@exec)
/*
编号 日期 时间1 时间2 时间3
------------------------------------------------
001 2008-02-02 20:00 21:00 23:00
*/