In the same way as MIN(), and AVG()I sometimes need the most common value (AKA-mode) Integer. This can be obtained as such:
SELECT column, COUNT(*) AS x
FROM table
GROUP BY column
ORDER BY x DESC
LIMIT 1
And this is a pretty sip. I would really like to use it as:MODE()
SELECT AVG(`sTemperature`), MODE(`sSwitch`), MODE(`sDoor`)
FROM `stats`;
Is there a way to make the above request work with integers? For boolean elements, the number is ROUND(AVG())beautiful.
Is it possible to define functions in a MySQL database, but can they work with datasets like MIN () or AVG ()?
Because I use it a lot inside:
INSERT INTO `statsaggregates` (
`saMeasurements`, `saTemperature`, `saSwitch`, `saDoor`
) SELECT
COUNT(*) as 'saMeasurements',
AVG(`sTemperature`) as 'saTemperatureAvg',
MIN(`sTemperature`) as 'saTemperatureMin',
MAX(`sTemperature`) as 'saTemperatureMax',
MODE(`sSwitch`) as 'saSwitch',
MODE(`sDoor`) as 'saDoor'
FROM `stats`
WHERE 1
. SO, MySQL, , . AVG() .