34,593
社区成员
发帖
与我相关
我的任务
分享
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 行)
*/
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 行)
*/
方法一
--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 行)
*/
--------------------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 行受影响)
*/
-- 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