好难的问题呀?有谁来接分呀???

Ypbird 2003-07-03 01:35:36
表数据是这样的:
A B C
001 501 1
002 502 1
003 503 1
004 401 2
005 402 2
................
如何才可以生成类似这样的数据格式:
1 501 502 503
2 401 402

...全文
38 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-07-05
  • 打赏
  • 举报
回复
--这个可以了.考虑实际情况,要用临时表处理

--创建数据测试环境
create table #tb (姓名 varchar(10), 房间号码 varchar(10),单位 varchar(10))
insert into #tb
select '001','101',1
union all select '002','110',1
union all select '003','120',1
union all select '004','202',1
union all select '004','212',1
union all select '004','302',1
union all select '005','606',2
union all select '006','706',2
union all select '006','755',2
union all select '006','346',2
union all select '007','118',3
union all select '007','128',3
select * from #tb

--得到想要的结果
declare @i int,@sql varchar(1000)

select @sql='',@i=aa
from(select top 1 单位,count(房间号码) as aa
from #tb group by 单位 order by count(房间号码) desc) a


while @i>0
select @sql=char(13)+',max(case a.id-b.id when '+cast(@i-1 as varchar)
+' then a.房间号码 end) as ['+cast(@i as varchar)
+']'+@sql
,@i=@i-1
set @sql='select a.单位'+char(13)+@sql
+char(13)+' from #temp a'
+char(13)+' ,(select 单位,min(id) as id from #temp group by 单位) b'
+char(13)+'where a.单位=b.单位'
+char(13)+'group by a.单位'
--print @sql
--为数据处理准备临时表
select id=identity(int,1,1),* into #temp from #tb
exec(@sql)

--删除测试数据
drop table #tb,#temp
friendwei 2003-07-05
  • 打赏
  • 举报
回复
呵呵...高手都来了!路过!顶!
Ypbird 2003-07-05
  • 打赏
  • 举报
回复
可能我的问题没有表达清楚?

姓名 房间号码 单位
001 101 1
002 110 1
003 120 1
004 202 1
005 606 2
006 706 2
007 118 3

返回结果:
1 101 110 120 202
2 606 706
3 118
Ypbird 2003-07-05
  • 打赏
  • 举报
回复
TO caiyunxia(monkey)


我是菜鸟呀,可否详细解释一下?为什么我在引用
select distinct RIGHT(B ,1) B INTO #1 from #
declare @sql varchar(1000)
set @sql =''
select @sql=@sql +',MAX(case when RIGHT(B,1) = ''' + RIGHT(B ,1) + ''' then B else null end) as ' + 'S'+RIGHT(B,1)
from #1
exec ('select c '+ @sql+ ' from # group by C')


只返回一列数据???

我并没有创建临时表#,而是直接引用已经生成的表
zjcxc 元老 2003-07-05
  • 打赏
  • 举报
回复
用存储过程来处理:
--考虑实际情况,要用临时表处理

--创建数据测试环境
create table #tb (姓名 varchar(10), 房间号码 varchar(10),单位 varchar(10))
insert into #tb
select '001','101',1
union all select '002','110',1
union all select '003','120',1
union all select '004','202',1
union all select '004','212',1
union all select '004','302',1
union all select '005','606',2
union all select '006','706',2
union all select '006','755',2
union all select '006','346',2
union all select '007','118',3
union all select '007','128',3
select * from #tb

go
--得到想要的结果
create procedure pbuild
as
declare @i int,@sql varchar(1000)

select @sql='',@i=aa
from(select top 1 单位,count(房间号码) as aa
from #tb group by 单位 order by count(房间号码) desc) a


while @i>0
select @sql=char(13)+',max(case a.id-b.id when '+cast(@i-1 as varchar)
+' then a.房间号码 end) as ['+cast(@i as varchar)
+']'+@sql
,@i=@i-1
set @sql='select a.单位'+char(13)+@sql
+char(13)+' from #temp a'
+char(13)+' ,(select 单位,min(id) as id from #temp group by 单位) b'
+char(13)+'where a.单位=b.单位'
+char(13)+'group by a.单位'
--print @sql
--为数据处理准备临时表
select id=identity(int,1,1),* into #temp from #tb
exec(@sql)

go

exec pbuild
--删除测试数据
drop table #tb
drop procedure pbuild
zjcxc 元老 2003-07-05
  • 打赏
  • 举报
回复
你可以将数据处理的部分改成存储过程来实现.

--原表数据
姓名 房间号码 单位
---------- ---------- ----------
001 101 1
002 110 1
003 120 1
004 202 1
004 212 1
004 302 1
005 606 2
006 706 2
006 755 2
006 346 2
007 118 3
007 128 3

(所影响的行数为 12 行)


处理后得到的结果:

单位 1 2 3 4 5 6
---------- ---------- ---------- ---------- ---------- ---------- ----------
1 101 110 120 202 212 302
2 606 706 755 346 NULL NULL
3 118 128 NULL NULL NULL NULL

caiyunxia 2003-07-03
  • 打赏
  • 举报
回复
19191919(红围巾) 也可以
用函数
caiyunxia 2003-07-03
  • 打赏
  • 举报
回复
c S1 S2 S3
---------- ---------- ---------- ----------
1 501 502 503
2 401 402 NULL
caiyunxia 2003-07-03
  • 打赏
  • 举报
回复

create table # (a varchar(10), b varchar(10),C VARCHAR(10))

INSERT INTO # VALUES('001', '501', '1')
INSERT INTO # VALUES('002', '502', '1')
INSERT INTO # VALUES('003', '503', '1')
INSERT INTO # VALUES('004', '401', '2')
INSERT INTO # VALUES('005', '402', '2')
SELECT * FROM #

select distinct RIGHT(B ,1) B INTO #1 from #
declare @sql varchar(1000)
set @sql =''
select @sql=@sql +',MAX(case when RIGHT(B,1) = ''' + RIGHT(B ,1) + ''' then B else null end) as ' + 'S'+RIGHT(B,1)
from #1
exec ('select c '+ @sql+ ' from # group by C')
CrazyFor 2003-07-03
  • 打赏
  • 举报
回复
参考:

create table #(a varchar(100),b int)
insert # values('aa',11)
insert # values('bb',1)
insert # values('aa',45)
insert # values('cc',81)
insert # values('a',11)
insert # values('aay',561)
insert # values('a',14)

declare @sql varchar(8000)
set @sql = 'select '
select @sql = @sql + 'sum(case a when '''+a+'''
then b else 0 end) '+a+'的数量,'
from (select distinct a from #) as a

select @sql = left(@sql,len(@sql)-1) + ' from #'

exec(@sql)

drop table #
CrazyFor 2003-07-03
  • 打赏
  • 举报
回复
对不起,看错题了,我上面的代码不对.:)
19191919 2003-07-03
  • 打赏
  • 举报
回复
create function f_getb(@c int)
returns varchar(200)
as
begin
declare @v varchar(200)
set @v=''
select @v=@v+space(2)+rtrim(b) from your_table
where c=@c
return @v
end
--调用
select c,dbo.f_getb(c) from your_table
group by c
CrazyFor 2003-07-03
  • 打赏
  • 举报
回复
更正:


declare @str varchar(8000)
set @str='select * from ('
select @str=@str+' select 固定字段,'+name +' from 表 union all ' from syscolumns where object_id('表')=id and Name<>'固定字段名' ---这里要去除固定字段
if @str<>''
set @str=left(@str,len(@str)-10)
set @str=@str +')tem order by 固定字段'

exec(@str)

CrazyFor 2003-07-03
  • 打赏
  • 举报
回复
declare @str varchar(8000)
set @str='select * from ('
select @str=@str+' select 固定字段,'+name +' from 表 union all ' from syscolumns where object_id('表')=id and colorder >1
if @str<>''
set @str=left(@str,len(@str)-10)
set @str=@str +')tem order by 固定字段'

exec(@str)
caiyunxia 2003-07-03
  • 打赏
  • 举报
回复
上面不对
caiyunxia 2003-07-03
  • 打赏
  • 举报
回复
declare @sql varchar(100)
set @sql =''
select @sql=@sql +',max(case when b = ''' + b + ''' then b else null end as ' + b
from (select distinct b from table ) aa

exec ('select c '+ @sql+ ' from table group by a')

34,590

社区成员

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

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