计算某列的字符串相加,求一个sql语句

Even713 2009-11-12 01:30:09
一个表
两列:
column1 char(12)
column2 int

举例:
column1 column2
—————————————
kk 1
gg 1
erew 2
43243 3

要求:以column2为聚合标准,把column1所有字符串相加

最后得到

----------
kk,gg 1
erew 2
43243 3
...全文
1113 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
vbmscommvs 2010-05-24
  • 打赏
  • 举报
回复
好深奥啊
RYAN--333 2009-11-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fredrickhu 的回复:]
SQL code合并列值--*******************************************************************************************表结构,数据如下:
id value----- ------1 aa1 bb2 aaa2 bbb2 ccc

需要得到结果:
idvalues---?-
[/Quote]
顶,学习了。
NDDavid 2009-11-12
  • 打赏
  • 举报
回复
看高手的解答
cxmcxm 2009-11-12
  • 打赏
  • 举报
回复
写一函数
SQL77 2009-11-12
  • 打赏
  • 举报
回复
占位接分,就是合并函数,看楼主应该是2000的
nightgoblin 2009-11-12
  • 打赏
  • 举报
回复
好高深啊,学习了。。。。看得头晕~~~
tlzmq 2009-11-12
  • 打赏
  • 举报
回复
可以
ws_hgo 2009-11-12
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 even713 的回复:]
消息 170,级别 15,状态 1,第 1 行
第 1 行: 'xml' 附近有语法错误。
是不是只能在2005上用啊?
我的是2000的数据库。
太菜了,不好意思。
[/Quote]

if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([column1] varchar(5),[column2] int)
insert [tb]
select 'kk',1 union all
select 'gg',1 union all
select 'erew',2 union all
select '43243',3

--sql2005
select distinct [column2],
stuff((select ','+[column1] from [tb] where b.[column2]=[column2] for xml path('')),1,1,'') [column1]
from [tb] b

--sql200
create function dbo.FC(@column2 int)
returns nvarchar(100)
as
begin
declare @sql nvarchar(100)
set @sql=''
select @sql=@sql+','+[column1] from [tb] where [column2]=@column2
return stuff(@sql,1,1,'')
end

select distinct dbo.FC([column2]) [column1],[column2] from [tb]
column1 column2
---------------------------------------------------------------------------------------------------- -----------
43243 3
erew 2
kk,gg 1
bancxc 2009-11-12
  • 打赏
  • 举报
回复
哇!都是高手! 看看,学习啦
feixianxxx 2009-11-12
  • 打赏
  • 举报
回复
,,,,,
gaomiqzhi 2009-11-12
  • 打赏
  • 举报
回复
学习
yang677888 2009-11-12
  • 打赏
  • 举报
回复
哇!都是高手! 看看,学习啦
--小F-- 2009-11-12
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-12 13:36:33
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([column1] varchar(5),[column2] int)
insert [tb]
select 'kk',1 union all
select 'gg',1 union all
select 'erew',2 union all
select '43243',3
--------------开始查询--------------------------
CREATE FUNCTION dbo.f_strUnite(@id varchar(10))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @str varchar(8000)
SET @str = ''
SELECT @str = @str + ',' + ltrim(column1) FROM tb WHERE column2=@id
RETURN STUFF(@str, 1, 1, '')
END
GO
-- 调用函数
SELECt column1 = dbo.f_strUnite(column2),column2 FROM tb GROUP BY column2
drop table tb
drop function dbo.f_strUnite
go


----------------结果----------------------------
/* column1 column2
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
kk,gg 1
erew 2
43243 3

(3 行受影响)
*/
Even713 2009-11-12
  • 打赏
  • 举报
回复
哦,楼上的可以用的。
fenshm 2009-11-12
  • 打赏
  • 举报
回复
上面已经有答案了。!
Even713 2009-11-12
  • 打赏
  • 举报
回复
消息 170,级别 15,状态 1,第 1 行
第 1 行: 'xml' 附近有语法错误。
是不是只能在2005上用啊?
我的是2000的数据库。
太菜了,不好意思。
jia_guijun 2009-11-12
  • 打赏
  • 举报
回复
--SQL 2000
if object_id('test') is not null
drop table test
go
Create table test(Col1 varchar(12),Col2 int)
insert test values('kk',1)
insert test values('gg',1)
insert test values('erew',2)
insert test values('43243',3)
GO
--创建一个函数
Create function [dbo].[fn_test]
(@Col2 int)
returns varchar(20)
as
begin
declare @str varchar(20)
select @str=isnull(@str+',','')+Col1 from test where Col2=@Col2
return @str
end
GO
--查询
select Col1=dbo.fn_test(Col2),
col2
from test a
group by col2

/*
Col1 col2
----------- -----------
kk,gg 1
erew 2
43243 3
*/
烈火蜓蜻 2009-11-12
  • 打赏
  • 举报
回复
最好的方法就是写一个聚合函数,
bancxc 2009-11-12
  • 打赏
  • 举报
回复
[code=SQL]if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([column1] varchar(5),[column2] int)
insert [tb]
select 'kk',1 union all
select 'gg',1 union all
select 'erew',2 union all
select '43243',3



select distinct x=(select ''+[column1] from tb where t.[column2]=[column2] for xml path('')),[column2]
from tb t
/*
43243 3
erew 2
kkgg 1*/[/CODE]
jia_guijun 2009-11-12
  • 打赏
  • 举报
回复
--SQL 2005
declare @t table(Col1 varchar(12),col2 int)
insert @t values('kk',1)
insert @t values('gg',1)
insert @t values('erew',2)
insert @t values('43243',3)

select Col1=stuff((select ','+Col1 from @t where a.Col2=Col2 for xml path('')),1,1,''),
col2
from @t a
group by col2

/*
Col1 col2
----------- -----------
kk,gg 1
erew 2
43243 3
*/
加载更多回复(7)

34,591

社区成员

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

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