在SQL中计算具有相同列的多个行

时间:2017-11-29 02:16:45

标签: mysql sql count

我正在尝试计算/添加具有相同列值的多行。

举个例子:

aid | uid | name              | region         | timestamp
17  | 43  | Max Payne         | Western Europe | 1507677429
101 | 91  | Jeffrey Birchwood | North America | 1509586239
45  | 109 | Richard Branson   | North America | 1509536026
167 | 133 | Soenke Tapaz      | North America | 1510603831
23  | 431 | Sophie Turner     | Western Europe | 1508166970
187 | 431 | Sophie Turner     | Western Europe | 1511190350
19  | 431 | Sophie Turner     | Western Europe | 1507887993
169 | 431 | Sophie Turner     | Western Europe | 1510612773
161 | 431 | Sophie Turner     | Western Europe | 1510574362
207 | 431 | Sophie Turner     | Western Europe | 1511788946
145 | 431 | Sophie Turner     | Western Europe | 1510138206
189 | 431 | Sophie Turner     | Western Europe | 1511196429
8   | 445 | Faisal Riaz       | North America | 1507583110
33  | 445 | Faisal Riaz       | North America | 1509480510
12  | 445 | Faisal Riaz       | North America | 1507659660
217 | 445 | Faisal Riaz       | North America | 1511914655
7   | 445 | Faisal Riaz       | North America | 1507560542
25  | 449 | Venkat Puri       | North America | 1508509764
121 | 449 | Venkat Puri       | North America | 1509704227
119 | 449 | Venkat Puri       | North America | 1509704171
117 | 449 | Venkat Puri       | North America | 1509703793
5   | 463 | Scott Jay         | North America | 1507301571
21  | 463 | Scott Jay         | North America | 1507910814
27  | 465 | Parker Tates      | EEMA          | 1509109957
29  | 467 | Vivian Bond       | Western Europe | 1509465890
31  | 469 | Jean-Christophe Glachant | Western Europe | 1509474304
35  | 471 | Arnab Goswami     | North America | 1509535475
37  | 473 | Matthias Toby     | Western Europe | 1509535623
39  | 475 | Shubham Sawdhan   | North America | 1509535625
41  | 477 | Martin Hoffmann   | Asia | 1509535626

目标

我要做的是计算每次出现region。因此我的结果将是:

Region           | Count

Western Europe   | 12
North America    | 16
Asia             | 1
EEMA             | 1

由于

1 个答案:

答案 0 :(得分:2)

您只需使用group by

select region, count(*)
from t
group by region;

我不知道您的查询与问题有什么关系。