紧急求援--如何求在一张表中,具有相同字段a的值的所有记录的b字段的和

iloveyoubaby 2009-11-03 04:18:45
比如我有一张表,有a,b两个字段,
cpdm cpxse
06100100 44767
06100100 53215.2
06100200 13241
06100200 12315
06100200 13244
06100200 56898
06100200 464747
06100200 3467
06100200 45477
请问如何将具有相同cpdm值的记录的cpxse值求和,也就是说上面这些记录进行求和处理后只有两条记录

...全文
26 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xiequan2 2009-11-03
select cpdm,sum(cpxse) as cpxse from tb group by cpdm
回复
SQL77 2009-11-03
/*
-- Author:SQL77--RICHIE
-- Version:V1.001 Date:2008-05-15--转Flystone

*/

-- Test Data: TB
If object_id('TB') is not null
Drop table TB
Go
Create table TB(cpdm int,cpxse int)
Go
Insert into TB
select 06100100,44767 union all
select 06100100,53215.2 union all
select 06100200,13241 union all
select 06100200,12315 union all
select 06100200,13244 union all
select 06100200,56898 union all
select 06100200,464747 union all
select 06100200,3467 union all
select 06100200,45477
Go
--Start
SELECT cpdm,SUM(cpxse )AS cpxse FROM TB GROUP BY cpdm


--Result:
/*

(所影响的行数为 9 行)

cpdm cpxse
----------- -----------
6100100 97982
6100200 609389

(所影响的行数为 2 行)



*/
--End
回复
bancxc 2009-11-03
create table test(cpdm nvarchar(10),cpxse int )
insert into test select
'06100100',44767 union all select
'06100100',53215.2 union all select
'06100200',13241 union all select
'06100200',12315 union all select
'06100200',13244 union all select
'06100200',56898 union all select
'06100200',464747 union all select
'06100200',3467 union all select
'06100200',45477

select cpdm,cpxse=SUM(cpxse)
from test
group by cpdm
/*cpdm cpxse
---------- -----------
06100100 97982
06100200 609389

(2 行受影响)
*/

drop table test
回复
--小F-- 2009-11-03
不好意思 写掉了from tb
回复
--小F-- 2009-11-03
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-03 16:20:08
-- 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]([cpdm] varchar(8),[cpxse] numeric(7,1))
insert [tb]
select '06100100',44767 union all
select '06100100',53215.2 union all
select '06100200',13241 union all
select '06100200',12315 union all
select '06100200',13244 union all
select '06100200',56898 union all
select '06100200',464747 union all
select '06100200',3467 union all
select '06100200',45477
--------------开始查询--------------------------
select cpdm,sum(cpxse) as cpxse from tb group by cpdm
----------------结果----------------------------
/* cpdm cpxse
-------- ---------------------------------------
06100100 97982.2
06100200 609389.0

(2 行受影响)
*/
回复
SQL77 2009-11-03
SELECT cpdm,SUM(cpxse )AS cpxse  FROM TB GROUP BY cpdm
回复
--小F-- 2009-11-03
select cpdm,sum(cpxse) as cpxse group by cpdm
回复
相关推荐
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2009-11-03 04:18
社区公告
暂无公告