求sql语句

会飞的小洋洋 2006-10-17 10:58:00
--产生基础数据
create table tb(uid int,a int,b int)
insert into tb select 1,1001,10011 union select 2,1001,10012 union select 3,1002,10021 union select 4,1002,10022
union select 5,1002,10023 union select 6,1003,10031 union select 7,1003,10032 union select 8,1003,10033
---希望结果
a b
1001 10011.10012
1002 10021.10022.10023
1003 10031.10032.10033
...全文
170 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
会飞的小洋洋 2006-10-17
  • 打赏
  • 举报
回复
好,谢谢各位,现在就结
OracleRoob 2006-10-17
  • 打赏
  • 举报
回复
这个问题,在SQL Server 2000中写函数,是最简单的处理方法。
子陌红尘 2006-10-17
  • 打赏
  • 举报
回复
在SQL Server 2000下不用函数比较麻烦,不推荐游标。
子陌红尘 2006-10-17
  • 打赏
  • 举报
回复
--产生基础数据
create table tb(uid int,a int,b int)
insert into tb select 1,1001,10011 union select 2,1001,10012 union select 3,1002,10021 union select 4,1002,10022
union select 5,1002,10023 union select 6,1003,10031 union select 7,1003,10032 union select 8,1003,10033
go

create function f_str(@a int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + '.' + cast(b as varchar) from tb where a = @a
set @str = stuff(@str,1,1,'')
return(@str)
end
go

select a ,dbo.f_str(a) from tb group by a
go

/*
1001 10011.10012
1002 10021.10022.10023
1003 10031.10032.10033
*/

drop function f_str
drop table tb
会飞的小洋洋 2006-10-17
  • 打赏
  • 举报
回复
谢谢,但是能不用函数吗
子陌红尘 2006-10-17
  • 打赏
  • 举报
回复
--生成测试数据
create table 表(部门 int,人员 varchar(20))
insert into 表 select 1,'张三'
insert into 表 select 1,'李四'
insert into 表 select 1,'王五'
insert into 表 select 2,'赵六'
insert into 表 select 2,'邓七'
insert into 表 select 2,'刘八'
go

--创建用户定义函数
create function f_str(@department int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+','+人员 from 表 where 部门 = @department
set @ret = stuff(@ret,1,1,'')
return @ret
end
go


--执行
select 部门,人员=dbo.f_str(部门) from 表 group by 部门 order by 部门
go

--输出结果
/*
部门 人员
---- --------------
1 张三,李四,王五
2 赵六,邓七,刘八
*/


--删除测试数据
drop function f_str
drop table 表
go
dawugui 2006-10-17
  • 打赏
  • 举报
回复
create function f_rowtocol(@a int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(b as varchar) from tb where a = @a
set @str = right(@str , len(@str) - 1)
return(@str)
End
go
select distinct a ,dbo.f_rowtocol(a) from tb

a
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1001 10011,10012
1002 10021,10022,10023
1003 10031,10032,10033

(所影响的行数为 3 行)
dawugui 2006-10-17
  • 打赏
  • 举报
回复
SQL语句之合并行列转换

  有表rowtocol,其数据如下:
  a b
  1 1
  1 2
  1 3
  2 1
  2 2
  3 1
  如何转换成如下结果:
  a b
  1 1,2,3
  2 1,2
  3 1

  创建一个合并的函数   

  create function f_rowtocol(@a int)
  returns varchar(8000)
  as
  begin
   declare @str varchar(8000)
   set @str = ''
   select @str = @str + ',' + cast(b as varchar) from rowtocol where a = @a
   set @str = right(@str , len(@str) - 1)
   return(@str)
  End
  go


  调用自定义函数得到结果:
  select distinct a ,dbo.f_rowtocol(a) from rowtocol

34,838

社区成员

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

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