首页 新闻 会员 周边

紧急请教SQL语句

0
悬赏园豆:10 [已解决问题] 解决于 2011-02-11 14:41

table 如下:

 OrgID   SubOrgID  Code

1            1               1111

1            3                1212

1            4                4541

5            5                2121

5            6                1010

7            7                3212

输出如下:

1111      1111,1212,4541

2121      2121,1010

3212      3212

谢谢了。。。紧急中。。。

 

 

doo的主页 doo | 初学一级 | 园豆:8
提问于:2011-01-20 14:09
< >
分享
最佳答案
0

因不知道字段类型,故建表结构如下:

orgid         int

suborgid   int

code         int

执行查询如下:

select code,STUFF((select ',' + convert(nvarchar,code)
from [org]
where orgid=o1.orgid
for xml path('')),1,1,'') as need
from org o1
where suborgid in (
select min(suborgid)
from org
group by orgid
)
输出:

1111    1111,1212,4541
2121    2121,1010
3212    3212

收获园豆:10
Localhost | 菜鸟二级 |园豆:443 | 2011-01-20 15:33
其他回答(1)
0

create function test_str
(
@id int
)
returns varchar(100)
as
 begin
     declare @a varchar(100)
        set @a=''
        select @a= @a+Code+',' from test where orid=@id --in(select distinct orid from test )
        return @a
 end
go
select distinct left(dbo.test_str(orid),charindex(',',dbo.test_str(orid))-1) ,dbo.test_str(orid) as code from test

秋夜北 | 园豆:470 (菜鸟二级) | 2011-01-20 16:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册