日期:2014-05-17 浏览次数:20880 次
select ID,Name,sum(num) as num
from 表A
group by ID,Name
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-08-14 10:19:31
-- Version:
-- Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64)
-- Jun 10 2013 20:09:10
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([ID] int,[name] varchar(4),[num] int)
insert [A]
select 1,'电脑',2000 union all
select 2,'手机',3000 union all
select 1,'电脑',223 union all
select 2,'手机',423
--------------开始查询--------------------------
select [id],[name],SUM([num])[num]
from [A]
GROUP BY [id],[name]
----------------结果----------------------------