Get a table with a value as key and a count or sum of another column, only of rows with that value

its_meow :

I'm very new to SQL, so I'm having trouble finding the right tools to do what I need. I have a table that looks like this

+----+--------------------------------------+---------------------+---------------------+------------+---------+
| id | uuid                                 | added               | expires             | multiplier | server  |
+----+--------------------------------------+---------------------+---------------------+------------+---------+
|  1 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:04:59 | 2020-04-25 20:04:59 |          1 | default |
|  2 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:05:59 | 2020-04-25 20:05:59 |          1 | default |
|  3 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:07:30 | 2020-04-25 20:07:30 |          1 | default |
|  4 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:12:12 | 2020-04-25 20:12:12 |          1 | default |
|  5 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:13:12 | 2020-04-25 20:13:12 |          1 | default |
|  6 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:14:12 | 2020-04-25 20:14:12 |          1 | default |
|  7 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:15:12 | 2020-04-25 20:15:12 |          1 | default |
|  8 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:16:12 | 2020-04-25 20:16:12 |          1 | default |
|  9 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:17:12 | 2020-04-25 20:17:12 |          1 | default |
| 10 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:18:12 | 2020-04-25 20:18:12 |          1 | default |
| 11 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:19:12 | 2020-04-25 20:19:12 |          1 | default |
| 12 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:20:13 | 2020-04-25 20:20:13 |          1 | default |
| 13 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:21:12 | 2020-04-25 20:21:12 |          1 | default |
| 14 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:22:12 | 2020-04-25 20:22:12 |          1 | default |
| 15 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:23:13 | 2020-04-25 20:23:13 |          1 | default |
| 16 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:24:12 | 2020-04-25 20:24:12 |          1 | default |
| 17 | 81d9726a-56d4-4419-9a2a-be1d7f7f7ef1 | 2020-03-25 20:25:13 | 2020-04-25 20:25:13 |          1 | default |
+----+--------------------------------------+---------------------+---------------------+------------+---------+

A want a query that given a UUID, returns a sum of the multiplier column AND a count of rows, with the key being the server name, like so:

+---------+-------+------+
| server  | count | sum  |
+---------+-------+------+
| default |   17  |  17  |
+---------+-------+------+

This is using the provided table, but if I were to use different values for server, the count and sum would be different.

How could I go about doing this?

tcadidot0 :

Try this then:

SELECT `Server`,COUNT(*),SUM(`multiplayer`) 
FROM `table_name` 
WHERE `UUID`='81d9726a-56d4-4419-9a2a-be1d7f7f7ef1'
GROUP BY `Server`;

*I include WHERE UUID

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360159&siteId=1