行列轉換

bb_chen 2010-03-05 09:45:31
有表tb結構如下:
partnumber Assembly
a ab
a ac
a ad
a ae
b ab
b bb
......

現在要得到如下的結果
a ab ac ad ae
b ab bb
...

注意:可能某個partnumber它對應1000個assembly,
而另外的一個partnumber只對應1個assemblt。

謝謝
...全文
127 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2010-03-05
  • 打赏
  • 举报
回复
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
if object_id('Tempdb..#') is not null
drop table #
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]')
中国风 2010-03-05
  • 打赏
  • 举报
回复
引用 7 楼 bb_chen 的回复:
引用 4 楼 roy_88 的回复:
SQL codeuse Tempdbgo--> -->ifnotobject_id(N'tb')isnulldroptable tbGoCreatetable tb([partnumber]nvarchar(1),[Assembly]nvarchar(2))Insert tbselect N'a',N'ab'unionallselect N'a',N'ac'unionallselect N'a',?-


運行無法通過

出錯信息是?

如果樓主是復制所有代碼,注意go的轉行格式,這是CSDN的格式復制問題

我在SQL2000、2005都正常
xing020010 2010-03-05
  • 打赏
  • 举报
回复
合并之后你要在一列里显示还是多列显示
dawugui 2010-03-05
  • 打赏
  • 举报
回复
--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 行受影响)
*/
bb_chen 2010-03-05
  • 打赏
  • 举报
回复
引用 4 楼 roy_88 的回复:
SQL codeuse Tempdbgo--> -->ifnotobject_id(N'tb')isnulldroptable tbGoCreatetable tb([partnumber]nvarchar(1),[Assembly]nvarchar(2))Insert tbselect N'a',N'ab'unionallselect N'a',N'ac'unionallselect N'a',?-



運行無法通過
fskjb01 2010-03-05
  • 打赏
  • 举报
回复
有点晕,标记下来慢慢看
中国风 2010-03-05
  • 打赏
  • 举报
回复
[partnumber],[Assembly]--不改變順序或存在重復值時,只能通過生成一個順序列處理
中国风 2010-03-05
  • 打赏
  • 举报
回复
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]')
ws_hgo 2010-03-05
  • 打赏
  • 举报
回复
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
mxc1225 2010-03-05
  • 打赏
  • 举报
回复
select
stuff((select ';'+col from tb where col2=t.col2 for xml path('')),1,1,''),
col2
from
tb t
group by
col2
中国风 2010-03-05
  • 打赏
  • 举报
回复
用動態寫法,一行行新增到另一表

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧