比如我有一张表,有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值求和,也就是说上面这些记录进行求和处理后只有两条记录
cpdm cpxse
06100100 44767
06100100 53215.2
06100200 13241
06100200 12315
06100200 13244
06100200 56898
06100200 464747
06100200 3467
06100200 45477
请问如何将具有相同cpdm值的记录的cpxse值求和,也就是说上面这些记录进行求和处理后只有两条记录
-- 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 行受影响)
*/
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
-- 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