Custom MODE () function used as MIN () or AVG () in MySQL

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() .

+3
1

, , "" MySQL UDF. - ​​ C/++, SQL:

UDF C ++ . MySQL sql/udf_example.c, UDF . , , UDF. include/mysql_com.h , UDF, , ; mysql.h.

+3

All Articles