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?
Here is one way to do it. Following script was created based on the answer to this question Rank function in MySQL
The query assigns a ranking to all the rows for which distinct count has been computer. I have assigned a value of 2 in the
CASEexpressions. This denotes that the script will display the top 2 screen widths and the remaining will be clubbed into Other. You need to change the value according to your requirement. I have hard coded the value99999to group all the other rows.There might be a better way to do this but this is one of the ways I could make it to work.
Click here to view the demo in SQL Fiddle.
Script:
Output: