I have a table like this;
+----+---------+-------------+
| id | user_id | screenWidth |
+----+---------+-------------+
| 1 | 1 | 1366 |
| 2 | 1 | 1366 |
| 3 | 1 | 1366 |
| 4 | 1 | 1366 |
| 5 | 2 | 1920 |
| 6 | 2 | 1920 |
| 7 | 3 | 1920 |
| 8 | 4 | 1280 |
| 9 | 5 | 1280 |
| 10 | 6 | 1280 |
| 11 | 7 | 1890 |
| ...| ... | ... |
| ...| ... | ... |
| ...| ... | ... |
| 100| 6 | 1910 |
+----+---------+-------------+
Where there are lots of screenWidths, but 90% of them are equal to one of 5 values.
Using a query like:
SELECT screenwidth
, COUNT(DISTINCT user_id) AS screenwidthcount
FROM screenwidth
GROUP BY screenwidth
ORDER BY screenwidthcount;
(Thanks from How do I count only the first occurrence of a value?)
I get a nice count for the number of times a screenWidth has occurred, counting only once per user.
Is there a way to count the most popular screenWidths, then collect all the others in a category called "other" - that is to say, instead of the query above returning loads of rows, it returns 6, the first 5 being the first 5 it returns currently, the 6th being called other with the sum of the rest of the values?