表A
ID name num
1 电脑 2000
2 手机 3000
1 电脑 223
2 手机 423
... ... ...
查询后
ID name num
1 电脑 2223
2 手机 3423
8 个解决方案
#1
select ID,Name,sum(num) as num
from tb
group by ID,Name
from tb
group by ID,Name
#2
select ID,Name,sum(num) as num
from 表A
group by ID,Name
#3
----------------------------------------------------------------
-- 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]
----------------结果----------------------------
/*
id name num
----------- ---- -----------
1 电脑 2223
2 手机 3423
*/
#4
SELECT id, name=MIN(name), num=SUM(num)
FROM 表A
GROUP BY id
ORDER BY id
#5
楼上都对,分组即可,哈哈
#6
select name,SUM(num) as num from a group by id,name
#7
select name,SUM(num) as num from a group by id,name
#8
select ID,Name,sum(num) as num
from [表名]
group by ID,Name
#1
select ID,Name,sum(num) as num
from tb
group by ID,Name
from tb
group by ID,Name
#2
select ID,Name,sum(num) as num
from 表A
group by ID,Name
#3
----------------------------------------------------------------
-- 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]
----------------结果----------------------------
/*
id name num
----------- ---- -----------
1 电脑 2223
2 手机 3423
*/
#4
SELECT id, name=MIN(name), num=SUM(num)
FROM 表A
GROUP BY id
ORDER BY id
#5
楼上都对,分组即可,哈哈
#6
select name,SUM(num) as num from a group by id,name
#7
select name,SUM(num) as num from a group by id,name
#8
select ID,Name,sum(num) as num
from [表名]
group by ID,Name