34,593
社区成员
发帖
与我相关
我的任务
分享
--sql 2000用子函数实现
Create table tb([partnumber] nvarchar(1),[Assembly] nvarchar(2))
Insert tb
select N'a',N'ab' union all
select N'a',N'ac' union all
select N'a',N'ad' union all
select N'a',N'ae' union all
select N'b',N'ab' union all
select N'b',N'bb'
Go
declare @sql varchar(8000)
set @sql = 'select partnumber '
select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then Assembly else '''' end) [' + cast(px as varchar) + ']'
from (select distinct px from (select * , px = (select count(1) from tb where partnumber = t.partnumber and Assembly < t.Assembly) + 1 from tb t) m) as a
set @sql = @sql + ' from (select * , px = (select count(1) from tb where partnumber = t.partnumber and Assembly < t.Assembly) + 1 from tb t) m group by partnumber'
exec(@sql)
drop table tb
--sql 2005使用row_number实现
Create table tb([partnumber] nvarchar(1),[Assembly] nvarchar(2))
Insert tb
select N'a',N'ab' union all
select N'a',N'ac' union all
select N'a',N'ad' union all
select N'a',N'ae' union all
select N'b',N'ab' union all
select N'b',N'bb'
Go
declare @sql varchar(8000)
set @sql = 'select partnumber '
select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then Assembly else '''' end) [' + cast(px as varchar) + ']'
from (select distinct px from (select * , px = row_number() over(partition by partnumber order by Assembly) from tb) m) as a
set @sql = @sql + ' from (select * , px = row_number() over(partition by partnumber order by Assembly) from tb) m group by partnumber'
exec(@sql)
drop table tb
/*
partnumber 1 2 3 4
---------- ---- ---- ---- ----
a ab ac ad ae
b ab bb
(2 行受影响)
*/
use Tempdb
go
--> -->
if not object_id(N'tb') is null
drop table tb
Go
Create table tb([partnumber] nvarchar(1),[Assembly] nvarchar(2))
Insert tb
select N'a',N'ab' union all
select N'a',N'ac' union all
select N'a',N'ad' union all
select N'a',N'ae' union all
select N'b',N'ab' union all
select N'b',N'bb'
Go
Select *,ID=Identity(int,1,1) into # from tb
declare @i nvarchar(2),@s nvarchar(2000)
select top 1 @i=count(*),@s='' from # group by [partnumber] order by count(*) desc
while @i>0
select @s=',[Assembly'+@i+']=max(case when con='+@i+' then [Assembly] else '''' end)'+@s,@i=@i-1
exec('select [partnumber]'+ @s+' from (select *,con=(select count(*) from # where [partnumber]=a.[partnumber] and ID<=a.ID) from # a)b group by [partnumber]')
create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
select UserName,sum(case when Subject= '数学' then Score else 0 end) [数学],sum(case when Subject= '物理' then Score else 0 end) [物理],sum(case when Subject= '语文' then Score else 0 end) [语文]
declare @sql varchar(1000)
set @sql='select UserName'
select @sql=@sql+',sum(case when Subject= ''' +Subject+ ''' then Score else 0 end) ['+Subject+']' from (select distinct Subject from tb)a
set @sql = @sql + ' from tb group by UserName'
print @sql
exec(@sql)
--讲解:
--这个是第一次执行
select 姓名,max(case 课程 when '数学' then 分数 else 0 end) [数学],
--这个是第二次
select 姓名,max(case 课程 when '数学' then 分数 else 0 end) [数学], max(case 课程 when '物理' then 分数 else 0 end) [物理] ,
--这个是第三次
select 姓名,max(case 课程 when '数学' then 分数 else 0 end) [数学], max(case 课程 when '物理' then 分数 else 0 end) [物理] , max(case 课程 when '语文' then 分数 else 0 end) [语文]
--这个的数量来自于
(select distinct 课程 from tb)--这里只有3们课程
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
create function dbo.f_str(@id int) returns varchar(100)
as
begin
declare @str varchar(1000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return @str
end
go
--调用函数
select id , value = dbo.f_str(id) from tb group by id
drop function dbo.f_str
drop table tb
我只说一个地方
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
你把这个看懂就明白了
例如当@id=1
select @str = @str + ',' + cast(value as varchar) from tb where id = 1
把满足id=1的str值通过','累加
当id是动态的就是1或者2...是当满足1的查询完了,把值付给str之后
在查询满足2的直到所有的ID完为止
这样明白了吧
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2009/03/17/3999394.aspx
select
stuff((select ';'+col from tb where col2=t.col2 for xml path('')),1,1,''),
col2
from
tb t
group by
col2