I now want to add an additional function, calculate the Mode (most common value) of cat for each cellid and include that value in the array. If there is more than one mode or no mode then return 9 (cat can only have values of 1 to 8).

I've been reading around how to find the Mode and the common solution looks like this:

SELECT COUNT(*) frequency, cat, cellid
FROM rel
GROUP BY cat
ORDER BY COUNT(*) DESC
LIMIT 1

But this does not return the most common cat for each cellid just the most common cat in general.

I have been working using a sub-query

SELECT cellid, cat, COUNT( * )
FROM `rel`
GROUP BY cellid, cat

Which produces a row for each cellidcat combination, but I'm unsure how to then use this to find the total number of entries for each cellid and the the most common cat for each cellid

EDIT

I have made some further progress, I now have a working query to find the mode cat for each cellid