热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

[求助]sqlserver2005中如何累加字符串。。急?

比如这样:nameorderNo--------------------chenORD1CHENORD2CHENOR
比如这样:
name        orderNo
--------------------
chen        ORD1
CHEN        ORD2
CHEN        ORD3
WANG        ORD4
WANG        ORD5
WANG        ORD6
WANG        ORD7

最后得到结果:
name           order
-----------------------------------
chen           ORD1,ORD2,ORD3
WANG           ORD4,ORD5,ORD6,ORD7

用SQL语句可以做到吗?
谢谢。。。

19 个解决方案

#1


STUFF FOR XML

05

#2


SELECT * FROM(SELECT DISTINCT name FROM tb)A OUTER APPLY( 
        SELECT [orderno]= STUFF(REPLACE(REPLACE( 
            ( 
                SELECT orderno FROM tb N 
                WHERE name = A.name
                FOR XML AUTO 
            ), ' ', ''), 1, 1, '') 
)N 
drop table tb 

#3


if object_id('[T]') is not null drop table [T]
create table [T] (name varchar(4),orderNo varchar(4))
insert into [T]
select 'chen','ORD1' union all
select 'CHEN','ORD2' union all
select 'CHEN','ORD3' union all
select 'WANG','ORD4' union all
select 'WANG','ORD5' union all
select 'WANG','ORD6' union all
select 'WANG','ORD7'


select distinct name,
stuff((select ','+orderNo from T where T.name=t1.name for xml path('')),1,1,'')
from T T1

name 
---- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
chen ORD1,ORD2,ORD3
WANG ORD4,ORD5,ORD6,ORD7

(2 行受影响)

#4


if not object_id('tb') is null
drop table tb
Go
Create table tb([name] nvarchar(4),[orderNo] nvarchar(4))
Insert tb
select N'chen',N'ORD1' union all
select N'CHEN',N'ORD2' union all
select N'CHEN',N'ORD3' union all
select N'WANG',N'ORD4' union all
select N'WANG',N'ORD5' union all
select N'WANG',N'ORD6' union all
select N'WANG',N'ORD7'
Go
Select name,
       orderno=stuff((select ','+name
                      from tb
                      where name=t.name
                      for xml path('')),1,1,'') 
from tb t
group by name
/*
name orderno
---- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
chen chen,CHEN,CHEN
WANG WANG,WANG,WANG,WANG

(2 個資料列受到影響)
*/

#5



select name, [values]=stuff((select ','+orderno from tb t where name=tb.name for xml path('')), 1, 1, '') 
from tb 
group by name 


/*name values
---- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
chen ORD1,ORD2,ORD3
WANG ORD4,ORD5,ORD6,ORD7

(2 行受影响)*/

#6


引用 1 楼 sql77 的回复:
STUFF FOR XML

05

 简洁~

#7


create table #ta(name varchar(20),orderNo varchar(20))
insert #ta select 'chen' ,'ORD1'
insert #ta select 'CHEN' ,'ORD2'
insert #ta select 'CHEN' ,'ORD3'
insert #ta select 'WANG' ,'ORD4'
insert #ta select 'WANG' ,'ORD5'
insert #ta select 'WANG' ,'ORD6'
insert #ta select 'WANG' ,'ORD7'

select name,STUFF((select ','+orderNO from #ta where a.name=name for XML path('')),1,1,'')  as orderNo
from #ta a
group by name
name                 orderNo
-------------------- -------------------------
chen                 ORD1,ORD2,ORD3
WANG                 ORD4,ORD5,ORD6,ORD7

(2 行受影响)

#8


你们这帮流氓。。。。

#9


引用 8 楼 ldslove 的回复:
你们这帮流氓。。。。

#10



use PracticeDB
if object_id('[TB]') is not null 
drop table [TB]
create table [TB](name varchar(5),orderno varchar(5))
insert [TB]
select 'chen', 'ORD1' union all
select 'CHEN', 'ORD2' union all
select 'CHEN', 'ORD3' union all
select 'WANG', 'ORD4' union all
select 'WANG', 'ORD5' union all
select 'WANG', 'ORD6' union all
select 'WANG', 'ORD7'


select name ,stuff((select ','+orderno
from tb
where name=a.name for xml path('')),1,1,'')as [order]
from tb a
group by name

name order
chen ORD1,ORD2,ORD3
WANG ORD4,ORD5,ORD6,ORD7

#11


路过学习

#12


学习中   能否请楼上的说说 
select ','+orderno from tb where name=a.name for xml path('')  中 path('')的意思?

#13


用函数也很好
--测试
if object_id('tb') is not null     drop table tb
Go
Create table tb([name] varchar(4),[orderNo] varchar(4))
Insert tb
select 'che','ORD1' union all
select 'CHE','ORD2' union all
select 'CHE','ORD3' union all
select 'WANG','ORD4' union all
select 'WANG','ORD5' union all
select 'WANG','ORD6' union all
select 'WANG','ORD7'
Go

if object_id('f_str') is not null     drop function f_str
Go 
create function dbo.f_str(@name varchar(4)) returns varchar(80)
as
begin
    declare @str varchar(100)
    set @str = ''
    select @str = @str + ',' + cast(orderNo as varchar) from tb where name = @name
    set @str = right(@str , len(@str) - 1)
    return @str
end
go

--调用函数
select name , orderNo = dbo.f_str(name) from tb group by name

--结果
/*

(所影响的行数为 7 行)

name orderNo                                                                          
---- -------------------------------------------------------------------------------- 
che  ORD1,ORD2,ORD3
WANG ORD4,ORD5,ORD6,ORD7

(所影响的行数为 2 行)
*/

#14


我也是来学习的!看之后很是有用!!!!!!

#15


膜拜+学习

#16


学习中。。。

#17


学习,函数的一千就会,但是XML的学下

#18


引用 13 楼 xys_777 的回复:
用函数也很好

SQL code
--测试
if object_id('tb') is not null     drop table tb
Go
Create table tb([name] varchar(4),[orderNo] varchar(4))
Insert tb
select 'che','ORD1' union all
select 'CHE','ORD2' u……



用函数好像速度上,明显会慢很多。。

#19


...

推荐阅读
author-avatar
feixiang1563122
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有