27,580
社区成员
发帖
与我相关
我的任务
分享
select
type,
name=stuff((select '+'+name from tb where type=t.type for xml path('')),1,1,''),
sum(number) as number
from
tb t
group by
type
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([type] varchar(2),[name] varchar(3),[number] int)
insert [tb]
select 'pc','pc1',1 union all
select 'pc','pc2',2 union all
select 'pb','pb3',3 union all
select 'pb','pb4',2
create function dbo.FC_StrPlus(@type varchar(20))
returns varchar(100)
as
begin
declare @sql varchar(100)
set @sql=''
select @sql=@sql+'+'+[name] from [tb] where [type]=@type
return stuff(@sql,1,1,'')
end
select distinct [type],[name],sum([number]) [number]
from
(
select [type],dbo.FC_StrPlus([type]) [name] ,sum([number])[number] from tb group by [name],[type]
)tt group by [name],[type]
type name number
---- ---------------------------------------------------------------------------------------------------- -----------
pb pb3+pb4 5
pc pc1+pc2
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-03 10:36:54
-- 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]([type] varchar(2),[name] varchar(3),[number] int)
insert [tb]
select 'pc','pc1',1 union all
select 'pc','pc2',2 union all
select 'pb','pb3',3 union all
select 'pb','pb4',2
--------------开始查询--------------------------
CREATE FUNCTION dbo.f_strUnite(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @str varchar(8000)
SET @str = ''
SELECT @str = @str + '+' + name FROM tb WHERE type=@id
RETURN STUFF(@str, 1, 1, '')
END
GO
-- 调用函数
SELECt type, name = dbo.f_strUnite(type),sum(number) as number FROM tb GROUP BY id
drop table tb
drop function dbo.f_strUnite
go
----------------结果----------------------------
/* type name number
---- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
pb pb3+pb4 5
pc pc1+pc2 3
(2 行受影响)
*/
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-03 10:36:54
-- 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]([type] varchar(2),[name] varchar(3),[number] int)
insert [tb]
select 'pc','pc1',1 union all
select 'pc','pc2',2 union all
select 'pb','pb3',3 union all
select 'pb','pb4',2
--------------开始查询--------------------------
CREATE FUNCTION dbo.f_strUnite(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @str varchar(8000)
SET @str = ''
SELECT @str = @str + '+' + value FROM tb WHERE type=@id
RETURN STUFF(@str, 1, 1, '')
END
GO
-- 调用函数
SELECt type, name = dbo.f_strUnite(type),sum(number) as number FROM tb GROUP BY id
drop table tb
drop function dbo.f_strUnite
go
----------------结果----------------------------
/* type name number
---- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
pb pb3+pb4 5
pc pc1+pc2 3
(2 行受影响)
*/
--> 测试数据: #tb
if object_id('tb') is not null drop table tb
go
create table tb (type varchar(2),name varchar(3),number int)
insert into tb
select 'pc','pc1',1 union all
select 'pc','pc2',2 union all
select 'pb','pb3',3 union all
select 'pb','pb4',2
if object_id('f_str') is not null drop function f_str
go
create function f_str(@id varchar(40))
returns varchar(40)
as
begin
declare @s varchar(40)
select @s=isnull(@s+'+','')+name
from tb where type=@id
return @s
end
go
select type,name=dbo.f_str(type),number=sum(number) from tb group by type
type name number
---- ---------------------------------------- -----------
pb pb3+pb4 5
pc pc1+pc2 3
(2 行受影响)
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-03 10:36:54
-- 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]([type] varchar(2),[name] varchar(3),[number] int)
insert [tb]
select 'pc','pc1',1 union all
select 'pc','pc2',2 union all
select 'pb','pb3',3 union all
select 'pb','pb4',2
--------------开始查询--------------------------
select
[type],
name=stuff((select '+'+name from tb where type=t.type for xml path('')),1,1,''),
sum(number) as number
from
tb t
group by
[type]
----------------结果----------------------------
/* type name number
---- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
pb pb3+pb4 5
pc pc1+pc2 3
(2 行受影响)
*/
select
type,
name=stuff((select '+'+name from tb where type=t.type for xml path('')),1,1,''),
sum(number) as number
from
tb t
group by
type