I've got his table:
+----+------+---------+
| id | user | upgrade |
+----+------+---------+
| 0 | A | NULL |
+----+------+---------+
| 1 | B | NULL |
+----+------+---------+
| 2 | C | A |
+----+------+---------+
| 3 | A | NULL |
+----+------+---------+
I would like with an unique query to know the total of occurence grouped by user_id but also to know the total of the upgrades by user.
Something like that:
+------+------+---------+
| user | nb | nb_upgd |
+------+------+---------+
| A | 2 | 1 |
+------+------+---------+
| B | 1 | 0 |
+------+------+---------+
| C | 1 | 0 |
+------+------+---------+
I have tried this with no luck:
SELECT user, COUNT(*) AS nb,
COUNT(CASE WHEN t.user_id=upgrade THEN 1 END) AS nb_upgd
FROM table t GROUP BY user
Dlost?nbwith a value of 2, not 1?