现在有表1中的数据,希望将月份信息提出来作为新表的列,新表中的数据为旧表里的报告提交时间。报告人:nvarchar(50)
报告月份:datetime
报告时间:datetime表1(原始数据表)
报告人 报告月份 报告提交时间
报告人1 2008.8.1 2008.8.8
报告人2 2008.8.1 2008.8.9
报告人3 2008.8.1 2008.8.1
报告人1 2008.7.1 2008.7.8
报告人2 2008.7.1 2008.7.9
报告人3 2008.7.1 2008.7.10表2(期望结果表)
报告人 2008.7 2008.8
报告人1 2008.7.8 2008.8.8
报告人2 2008.7.9 2008.8.9
报告人3 2008.7.10 2008.8.10
报告月份:datetime
报告时间:datetime表1(原始数据表)
报告人 报告月份 报告提交时间
报告人1 2008.8.1 2008.8.8
报告人2 2008.8.1 2008.8.9
报告人3 2008.8.1 2008.8.1
报告人1 2008.7.1 2008.7.8
报告人2 2008.7.1 2008.7.9
报告人3 2008.7.1 2008.7.10表2(期望结果表)
报告人 2008.7 2008.8
报告人1 2008.7.8 2008.8.8
报告人2 2008.7.9 2008.8.9
报告人3 2008.7.10 2008.8.10
解决方案 »
- 2万条的数据要查询多久
- 触发器问题在线等
- 如何检查表中的记录是否是3分钟前的记录?
- 100分求一个SELECT语句,在线等。。。
- sql server management studio express 程序启动不了
- 紧急(触发器问题)
- 如何在select 获得记录集的同时update 该记录集
- 搜索某一字段值最大的那条记录的SQL语句怎么写啊?
- 请问索引可不可以建立在字段的部分内容上
- √当我在sql server 2000中更新记录时,系统报错:键列信息不足或不正确。更新影响到过多的行。请问这是为什么?
- Sql Server 数据库更新问题
- 问当MSSQL2005把DB 的恢复模式改为简单,但不知为何,有时DB 的log还是会增加?
set @sql=''select @sql=@sql+',['+convert(char(7),报告月份,120)+']=max(case 报告月份 when '''+convert(varchar(20),报告月份,120)+''' then 报告提交时间 end)'
from (select distinct 报告月份 from 表1) tset @sql='select 报告人'+@sql+' from 表1 group by 报告人'exec(@sql)
insert into tb values('报告人1', '2008.8.1' ,'2008.8.8' )
insert into tb values('报告人2', '2008.8.1' ,'2008.8.9' )
insert into tb values('报告人3', '2008.8.1' ,'2008.8.1' )
insert into tb values('报告人1', '2008.7.1' ,'2008.7.8' )
insert into tb values('报告人2', '2008.7.1' ,'2008.7.9' )
insert into tb values('报告人3', '2008.7.1' ,'2008.7.10')
goselect 报告人,
max(case convert(varchar(7),报告提交时间,120) when '2008-07' then 报告提交时间 else null end) '2008-07',
max(case convert(varchar(7),报告提交时间,120) when '2008-08' then 报告提交时间 else null end) '2008-08'
from tb
group by 报告人drop table tb/*报告人 2008-07 2008-08
-------------------- ------------------------------------------------------ ------------------------------------------------------
报告人1 2008-07-08 00:00:00.000 2008-08-08 00:00:00.000
报告人2 2008-07-09 00:00:00.000 2008-08-09 00:00:00.000
报告人3 2008-07-10 00:00:00.000 2008-08-01 00:00:00.000(所影响的行数为 3 行)
*/
if object_id('[表1]') is not null drop table [表1]
create table [表1] (报告人 varchar(7),报告月份 datetime,报告提交时间 datetime)
insert into [表1]
select '报告人1','2008.8.1','2008.8.8' union all
select '报告人2','2008.8.1','2008.8.9' union all
select '报告人3','2008.8.1','2008.8.1' union all
select '报告人1','2008.7.1','2008.7.8' union all
select '报告人2','2008.7.1','2008.7.9' union all
select '报告人3','2008.7.1','2008.7.10'
declare @sql varchar(8000)
set @sql='select 报告人'
select @sql=@sql+',['+报告月份+']=max(case convert(varchar(7),报告月份,102) when '''+报告月份+'''
then 报告提交时间 else null end)' from (select distinct 报告月份=convert(varchar(7),报告月份,102) from [表1])a
set @sql=@sql+' from 表1 group by 报告人'
exec(@sql)
--结果:
报告人 2008.07 2008.08
------- --------------------------- ------------------------------------
报告人1 2008-07-08 00:00:00.000 2008-08-08 00:00:00.000
报告人2 2008-07-09 00:00:00.000 2008-08-09 00:00:00.000
报告人3 2008-07-10 00:00:00.000 2008-08-01 00:00:00.000
insert into 表1 values('报告人1','2008.8.1','2008.8.8' )
insert into 表1 values('报告人2','2008.8.1','2008.8.9' )
insert into 表1 values('报告人3','2008.8.1','2008.8.1' )
insert into 表1 values('报告人1','2008.7.1','2008.7.8' )
insert into 表1 values('报告人2','2008.7.1','2008.7.9' )
insert into 表1 values('报告人3','2008.7.1','2008.7.10')
godeclare @sql varchar(8000)
set @sql=''select @sql=@sql+',['+convert(char(7),报告月份,120)+']=max(case 报告月份 when '''+convert(varchar(20),报告月份,120)+''' then 报告提交时间 end)'
from (select distinct 报告月份 from 表1) tset @sql='select 报告人'+@sql+' from 表1 group by 报告人'exec(@sql)
go/*
报告人 2008-07 2008-08
-------- ------------------------------------------------------ ------------------------------------------------------
报告人1 2008-07-08 00:00:00.000 2008-08-08 00:00:00.000
报告人2 2008-07-09 00:00:00.000 2008-08-09 00:00:00.000
报告人3 2008-07-10 00:00:00.000 2008-08-01 00:00:00.000
*/drop table 表1
go
insert into tb values('报告人1', '2008.8.1' ,'2008.8.8' )
insert into tb values('报告人2', '2008.8.1' ,'2008.8.9' )
insert into tb values('报告人3', '2008.8.1' ,'2008.8.1' )
insert into tb values('报告人1', '2008.7.1' ,'2008.7.8' )
insert into tb values('报告人2', '2008.7.1' ,'2008.7.9' )
insert into tb values('报告人3', '2008.7.1' ,'2008.7.10')
go
--sql静态语句。固定月份为2008-07,2008-08
select 报告人,
max(case convert(varchar(7),报告提交时间,120) when '2008-07' then 报告提交时间 else null end) '2008-07',
max(case convert(varchar(7),报告提交时间,120) when '2008-08' then 报告提交时间 else null end) '2008-08'
from tb
group by 报告人--SQL动态语句,月份不固定
declare @sql varchar(8000)
set @sql = 'select 报告人 '
select @sql = @sql + ' , max(case convert(varchar(7),报告提交时间,120) when ''' + 月份 + ''' then 报告提交时间 else 0 end) [' + 月份 + ']'
from (select distinct convert(varchar(7),报告提交时间,120) 月份 from tb) as a
set @sql = @sql + ' from tb group by 报告人'
exec(@sql) drop table tb/*报告人 2008-07 2008-08
-------------------- ------------------------------------------------------ ------------------------------------------------------
报告人1 2008-07-08 00:00:00.000 2008-08-08 00:00:00.000
报告人2 2008-07-09 00:00:00.000 2008-08-09 00:00:00.000
报告人3 2008-07-10 00:00:00.000 2008-08-01 00:00:00.000(所影响的行数为 3 行)
*/
set @sql=''
select @sql=@sql+',max(case when convert(varchar(7),报告月份,102)='''+convert(varchar(7),报告月份,102)+''' then 报告提交时间 end) as ['+convert(varchar(7),报告月份,102)+']'
from 原始数据表
group by convert(varchar(7),报告月份,102)exec('select 报告人'+@sql+' from 原始数据表 group by 报告人')
insert into tb select '报告人1','2008.8.1','2008.8.8'
insert into tb select '报告人2','2008.8.1','2008.8.9'
insert into tb select '报告人3','2008.8.1','2008.8.1'
insert into tb select '报告人1','2008.7.1','2008.7.8'
insert into tb select '报告人2','2008.7.1','2008.7.9'
insert into tb select '报告人3','2008.7.1','2008.7.10'declare @sql varchar(8000)
select @sql=isnull(@sql+',','')+'max(case when convert(varchar(7),报告月份,120)='''+convert(varchar(7),报告月份,120)+''' then convert(varchar(10),报告时间,120) else '''' end) as ['+convert(varchar(7),报告月份,120)+']'
from (select distinct convert(varchar(7),报告月份,120) as 报告月份 from tb)t
exec('select 报告人,'+@sql+' from tb group by 报告人')报告人 2008-07 2008-08
报告人1 2008-07-08 2008-08-08
报告人2 2008-07-09 2008-08-09
报告人3 2008-07-10 2008-08-01
(报告人 NVARCHAR(50),报告月份 DATETIME,报告时间 DATETIME )
INSERT tb SELECT '报告人1','2008.8.1','2008.8.8'UNION ALL SELECT
'报告人2','2008.8.1','2008.8.9'UNION ALL SELECT
'报告人3','2008.8.1','2008.8.1'UNION ALL SELECT
'报告人1','2008.7.1','2008.7.8' UNION ALL SELECT
'报告人2','2008.7.1','2008.7.9' UNION ALL SELECT
'报告人3','2008.7.1','2008.7.10'
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',['+convert(char(7),报告月份,120)+']=max(case 报告月份 when '''+convert(varchar(20),报告月份,120)+''' then 报告时间 end)'
from (select distinct 报告月份 from tb) t
set @sql='select 报告人'+@sql+' from tb group by 报告人'
exec(@sql)
报告人 2008-07 2008-08
-------------------------------------------------- ------------------------------------------------------ ------------------------------------------------------
报告人1 2008-07-08 00:00:00.000 2008-08-08 00:00:00.000
报告人2 2008-07-09 00:00:00.000 2008-08-09 00:00:00.000
报告人3 2008-07-10 00:00:00.000 2008-08-01 00:00:00.000
select @sql=isnull(@sql+',','')+'max(case when convert(varchar(7),报告月份,120)='''+convert(varchar(7),报告月份,120)+''' then replace(convert(varchar(10),报告时间,120),''-'',''.'') else '''' end) as ['+replace(convert(varchar(7),报告月份,120),'-','.')+']'
from (select distinct convert(varchar(7),报告月份,120) as 报告月份 from tb)t
exec('select 报告人,'+@sql+' from tb group by 报告人')报告人 2008.07 2008.08
报告人1 2008.07.08 2008.08.08
报告人2 2008.07.09 2008.08.09
报告人3 2008.07.10 2008.08.01