按条件合并多行的SQL句子

sjt000 2009-11-03 10:33:01
数据库中数据如下:
type name number

pc pc1 1
pc pc2 2
pb pb3 3
pb pb4 2
想要的结果:按type分类,number相加,name连接起来,如下:

type name number
pc pc1+pc2 3
pb pb3+pb4 5

如何写SQL~?
...全文
136 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fwacky 2009-11-03
  • 打赏
  • 举报
回复

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
ws_hgo 2009-11-03
  • 打赏
  • 举报
回复
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
luoyoumou 2009-11-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 josy 的回复:]
SQL codeselect
type,
name=stuff((select'+'+namefrom tbwhere type=t.typefor xml path('')),1,1,''),sum(number)asnumberfrom
tb tgroupby
type
[/Quote]

----向树哥致敬! SQL大师!
--小F-- 2009-11-03
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- 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 行受影响)

*/
--小F-- 2009-11-03
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- 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 行受影响)

*/
华夏小卒 2009-11-03
  • 打赏
  • 举报
回复

--> 测试数据: #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 行受影响)

--小F-- 2009-11-03
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- 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 行受影响)

*/
华夏小卒 2009-11-03
  • 打赏
  • 举报
回复
函数 or cursor
百年树人 2009-11-03
  • 打赏
  • 举报
回复
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

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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