1.先合并select 编号,类别+' '+面积 as '类别和面积' into #aa from 表名 2.处理--大力的create function getstr(@column2 int) returns varchar(8000) as begin declare @str varchar(8000) set @str='' select @str=@str+','+rtrim(column1) from 你的表 where column2=@column2 select @str=right(@str,len(@str)-1) where @str<>''return @str end goselect 编号,dbo.getstr(类别和面积) 类别和面积 from #aa group by 编号 drop table #aa go
假设都是字符型select a.编号, a.类别+a.面积+','+b.类别+b.面积 as 类别和面积 from table a, table b where a.编号 = b.编号 and a.类别<> b.类别 and a.面积<> b.面积不过这样选出来的结果多一组重复的行那位高手帮帮忙吧
当然可以,只要把函数改一改: create function getstr(@column2 int) returns varchar(8000) as begin declare @str varchar(8000) set @str='' select @str=@str+','+rtrim(类别)+convert(varchar(10),面积) from 你的表 where column2=@column2 select @str=right(@str,len(@str)-1) where @str<>''return @str select 编号,dbo.getstr(编号) 类别和面积 from 你的表 group by 编号
假如这个表存在主键就比较好办了select a.编号, a.类别+a.面积+','+b.类别+b.面积 as 类别和面积 from table a, table b where a.编号 = b.编号 and a.主键 < b.主键试验已通过
To: littlepotato(土豆土豆) 你只是看到了一个片面的。 不信你试试: create table #aa(no int identity(1,1),id int,type varchar(2),area int) insert #aa select 1,'a',50 union all select 1,'b',100 union all select 2,'c',20 union all select 2,'d',30 union all select 2,'e',100 select * from #aaselect a.id, a.type+' '+convert(varchar(10),a.area)+','+b.type+' '+convert(varchar(10),b.area) as area from #aa a, #aa b where a.id=b.id and a.no < b.no
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+rtrim(column1) from 你的表 where column2=@column2
select @str=right(@str,len(@str)-1) where @str<>''return @str
end
goselect 编号,dbo.getstr(类别和面积) 类别和面积 from #aa group by 编号
drop table #aa
go
create function getstr(@column2 int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+rtrim(类别)+convert(varchar(10),面积) from 你的表 where column2=@column2
select @str=right(@str,len(@str)-1) where @str<>''return @str
select 编号,dbo.getstr(编号) 类别和面积 from 你的表 group by 编号
你只是看到了一个片面的。
不信你试试:
create table #aa(no int identity(1,1),id int,type varchar(2),area int)
insert #aa select 1,'a',50
union all select 1,'b',100
union all select 2,'c',20
union all select 2,'d',30
union all select 2,'e',100
select * from #aaselect a.id, a.type+' '+convert(varchar(10),a.area)+','+b.type+' '+convert(varchar(10),b.area) as area from #aa a, #aa b where a.id=b.id and a.no < b.no
不知道楼主的表里面有没有超过两条记录的编号
别抱着巧幸的心理去做题,看问题也要全面一点,即使楼主的表中都只有两条记录的编号,那又怎么样呢,来这是学习的,向星星们多学习学习。