如何在SUM时将某列值串连?

hdf007 2010-05-01 09:54:54
表内容如下:
ID cWhCode iQuantity
5 01 15
6 03 23
7 01 76
11 01 32
12 03 58
15 04 9

如何通过SQL语句得到下列结果:

cWhCode iQuantity ID
01 123 5,7,11
03 81 6,12
04 9 15
...全文
63 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2010-05-02
  • 打赏
  • 举报
回复
--sql 2000用如下函数实现.
create table tb
(
ID int,
cWhCode char(3),
iQuantity int
)
go
--插入测试数据
insert into tb select 5,'01',15
union all select 6,'03',23
union all select 7,'01',76
union all select 11,'01',32
union all select 12,'03',58
union all select 15,'04',9
go

create function dbo.f_str(@cWhCode int) returns varchar(100)
as
begin
declare @str varchar(1000)
set @str = ''
select @str = @str + ',' + cast(ID as varchar) from tb where cWhCode = @cWhCode
set @str = right(@str , len(@str) - 1)
return @str
end
go

--调用函数
select cWhCode ,sum(iQuantity) iQuantity , ID = dbo.f_str(cWhCode) from tb group by cWhCode

drop function dbo.f_str

drop table tb

/*
cWhCode iQuantity ID
------- ----------- ---------
01 123 5,7,11
03 81 6,12
04 9 15

(所影响的行数为 3 行)
*/
永生天地 2010-05-01
  • 打赏
  • 举报
回复

create table tb(id int, cWhCode varchar(10),iQuantity int)
insert into tb values(5, '01',15)
insert into tb values(6, '03',23)
insert into tb values(7, '01',76)
insert into tb values(11, '01',32)
insert into tb values(12, '03',58)
insert into tb values(15, '04',9)
go

--在sql server 2000中用函数解决。
--1. 创建处理函数
CREATE FUNCTION dbo.f_str(@cWhCode int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SELECT @r = isnull(@r+',' ,'') + ltrim(id) FROM tb WHERE cWhCode=@cWhCode
RETURN @r
END
GO

-- 调用函数
SELECt cWhCode,iQuantity=sum(iQuantity),id = dbo.f_str(cWhCode) FROM tb GROUP BY cWhCode
/*
cWhCode iQuantity ID
---------- ----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01 123 5,7,11
03 81 6,12
04 9 15

(所影响的行数为 3 行)
*/
永生天地 2010-05-01
  • 打赏
  • 举报
回复
方法一

--SQL2005中的方法
create table tb(id int, cWhCode varchar(10),iQuantity int)
insert into tb values(5, '01',15)
insert into tb values(6, '03',23)
insert into tb values(7, '01',76)
insert into tb values(11, '01',32)
insert into tb values(12, '03',58)
insert into tb values(15, '04',9)
go

select
cWhCode,
iQuantity=sum(iQuantity) ,
[ID]=stuff((select ','+ltrim(ID) from tb t where cWhCode=tb.cWhCode for xml path('')), 1, 1, '')
from tb
group by cWhCode

/*
cWhCode iQuantity ID
---------- ----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01 123 5,7,11
03 81 6,12
04 9 15

(所影响的行数为 3 行)
*/
喜-喜 2010-05-01
  • 打赏
  • 举报
回复
--------------------SQL Server数据格式化工具-------------------
---------------------------------------------------------------
-- DESIGNER :happycell188(喜喜)
-- QQ :584738179
-- Development Tool :Microsoft Visual C++ 6.0 C Language
-- FUNCTION :CONVERT DATA TO T-SQL
---------------------------------------------------------------
-- Microsoft SQL Server 2005
-- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
---------------------------------------------------------------
---------------------------------------------------------------

use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
ID int,
cWhCode char(3),
iQuantity int
)
go
--插入测试数据
insert into tb select 5,'01',15
union all select 6,'03',23
union all select 7,'01',76
union all select 11,'01',32
union all select 12,'03',58
union all select 15,'04',9
go
--代码实现

select cWhCode,iQuantity=sum(iQuantity),
ID=stuff((select ','+rtrim(ID) from tb where cWhCode=t.cWhCode for xml path('')),1,1,'')
from tb t group by cWhCode

/*测试结果

cWhCode iQuantity ID
---------------------
01 123 5,7,11
03 81 6,12
04 9 15

(3 行受影响)
*/
xman_78tom 2010-05-01
  • 打赏
  • 举报
回复

-- SQL 2005
select cWhCode,
sum(iQuantity) iQuantity,
stuff((select ','+ltrim(id) from tab where t.cWhCode=cWhCode for xml path('')),1,1,',') id
from tab t
group by cWhCode

34,593

社区成员

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

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