Mysql - 摘要报告 - 删除重复项 - 列总数

时间:2015-08-23 13:02:59

标签: mysql

我有一个表保存设备,以及它们在Mysql中的关系。

单个设备可以与多个对象相关联。我想使用deviceId,因此我不会多次计算设备,并提供所有设备的总容量。

Array | Capacity | deviceId | Assignment
 4321    1024       3eb        esx1
 4321    1024       3eb        esx2
 4321    1024       3eb        esx3
 4321    28672      3ec        win1
 4321    61440      9f1        unassigned

我想要一份类似于以下内容的报告

Array | Capacity 
 4321    91136

我已经尝试了select sum(Capacity) from table group by deviceId,这并没有给我预期的结果。

非常感谢任何见解。

1 个答案:

答案 0 :(得分:0)

假设重复数据(前3列),如问题所示:

create table table1
(   id int auto_increment primary key,
    Array int not null,
    Capacity int not null,
    deviceId varchar(100) not null,
    Assignment varchar(100) not null
);
truncate table table1;
insert table1(array,capacity,deviceId,assignment) values 
(4321,1024,'3eb','blah'),
(4321,1024,'3eb','blah'),
(4321,1024,'3eb','blah'),
(4321,28672,'3ec','blah'),
(4321,61440,'9f1','blah');

select Array,sum(Capacity) as Capacity 
from 
( select distinct Array,Capacity,deviceId
  from table1
) xxx 
group by Array;

+-------+----------+
| Array | Capacity |
+-------+----------+
|  4321 |    91136 |
+-------+----------+
相关问题