22,209
社区成员
发帖
与我相关
我的任务
分享
/*
原表:news_pic
id news_id pic_name pic_shuoming
12 228 ../upfile/10.jpg haha..
13 30 ../upfile/15.jpg happy
18 30 ../upfile/25.jpg 来访客人
20 1588 ../upfile/26.jpg 来访客人
46 75 ../upfile/28.jpg 英国客人
...
转换:
news_id pic_name1 pic_shuoming1 pic_name2 pic_shuoming2 ...
228 ../upfile/10.jpg haha..
30 ../upfile/15.jpg happy ../upfile/25.jpg 来访客人
20 ../upfile/26.jpg 来访客人
46 ../upfile/28.jpg 英国客人
*/
--> 测试数据:tb
if object_id('tb') is not null drop table tb
create table tb([id] int,[news_id] int,[pic_name] varchar(16),[pic_shuoming] varchar(8))
insert tb
select 12,228,'../upfile/10.jpg','haha..' union all
select 13,30,'../upfile/15.jpg','happy' union all
select 18,30,'../upfile/25.jpg','来访客人' union all
select 20,1588,'../upfile/26.jpg','来访客人' union all
select 46,75,'../upfile/28.jpg','英国客人'
select (select count(1) from tb where news_id = t.news_id and [id] <= t.[id]) as group_id,*
into #temp from tb t
declare @sql varchar(8000)
select @sql = ''
select @sql = @sql + ',max(case group_id when '''+ltrim(group_id)+''' then pic_name else '''' end) as [pic_name'+ltrim(group_id)+'],
max(case group_id when '''+ltrim(group_id)+''' then pic_shuoming else '''' end) as [pic_shuoming'+ltrim(group_id)+']'
from (select distinct group_id from #temp) t
select @sql = 'select news_id'+@sql+' from #temp group by news_id order by max([id])'
exec(@sql)
drop table #temp
、列转行
--> --> (Roy)生成測試數據
if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)
Insert Class
select N'李四',77,85,65,65 union all
select N'张三',87,90,82,78
Go
--2000:
动态:
declare @s nvarchar(4000)
select @s=isnull(@s+' union all ','')+'select [Student],[Course]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字符串@s中第一个union all
+',[Score]='+quotename(Name)+' from Class'
from syscolumns where ID=object_id('Class') and Name not in('Student')--排除不转换的列
order by Colid
exec('select * from ('+@s+')t order by [Student],[Course]')--增加一个排序
生成静态:
select *
from (select [Student],[Course]='数学',[Score]=[数学] from Class union all
select [Student],[Course]='物理',[Score]=[物理] from Class union all
select [Student],[Course]='英语',[Score]=[英语] from Class union all
select [Student],[Course]='语文',[Score]=[语文] from Class)t
order by [Student],[Course]
go
--2005:
动态:
declare @s nvarchar(4000)
select @s=isnull(@s+',','')+quotename(Name)
from syscolumns where ID=object_id('Class') and Name not in('Student')
order by Colid
exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')
go
select
Student,[Course],[Score]
from
Class
unpivot
([Score] for [Course] in([数学],[物理],[英语],[语文]))b
生成格式:
/*
Student Course Score
------- ------- -----------
李四 数学 77
李四 物理 85
李四 英语 65
李四 语文 65
张三 数学 87
张三 物理 90
张三 英语 82
张三 语文 78
(8 行受影响)
*/
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([taName] varchar(20),[licNo] varchar(20),[turn] int,[year] int)
insert [tb]
select '111 222',2410001,10,200501 union all
select '111 222',2410001,10,200502 union all
select '111 222',2410001,10,200502 union all
select '111 222',2410001,0,200503 union all
select '111 222',2410001,null,200504 union all
select '111 222',2410001,null,200505 union all
select 'aa aa',2410002,50,200501 union all
select 'aa aa',2410002,50,200502 union all
select 'aa aa',2410002,50,200502 union all
select 'aa aa',2410002,0,200503 union all
select 'aa aa',2410002,null,200504 union all
select 'aa aa',2410002,null,200505 union all
select 'aa aa',2410002,null,200506 union all
select 'aa aa',2410002,null,200507 union all
select 'aa aa',2410002,null,200508 union all
select 'B11 b11',2410003,null,200801 union all
select 'B11 b11',2410003,null,200802 union all
select 'B11 b11',2410003,null,200802 union all
select 'B11 b11',2410003,0,200803 union all
select 'B11 b11',2410003,10,200804 union all
select 'B11 b11',2410003,null,200805
declare @sql varchar(8000)
select @sql=isnull(@sql+',','')+'max(case year when '+ltrim(year)+' then turn else null end) as ['+ltrim(year)+']'
from (select distinct top 100 percent year from [tb] order by year) t
select @sql = 'select taName,licNo,'+@sql+' from [tb] group by taName,licNo'
exec(@sql)
------------------------------------
111 222 2410001 10 10 0 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
aa aa 2410002 50 50 0 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
B11 b11 2410003 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 0 10 NULL