Collections:
MIN() - Minimum Value in Group
How to calculate the minimum value of a field expression in result set groups using the MIN() function?
✍: FYIcenter.com
MIN(expr) is a MySQL built-in aggregate function that calculates the minimum value of a field expression in result set groups. For example:
SELECT help_category_id, MIN(help_topic_id), COUNT(help_topic_id) FROM mysql.help_topic GROUP BY help_category_id; -- +------------------+--------------------+----------------------+ -- | help_category_id | MIN(help_topic_id) | COUNT(help_topic_id) | -- +------------------+--------------------+----------------------+ -- | 1 | 0 | 2 | -- | 2 | 2 | 35 | -- | 3 | 3 | 59 | -- | 4 | 4 | 2 | -- | 5 | 40 | 3 | -- ... -- +------------------+--------------------+----------------------+ SELECT help_category_id, help_topic_id FROM mysql.help_topic WHERE help_category_id = 5; -- +------------------+---------------+ -- | help_category_id | help_topic_id | -- +------------------+---------------+ -- | 5 | 40 | -- | 5 | 43 | -- | 5 | 44 | -- +------------------+---------------+
MIN() is also a window function, you can call it with the OVER clause to calculate the minimum value of the given expression in the current window. For example:
SELECT help_topic_id, help_category_id, MIN(help_topic_id) OVER w FROM mysql.help_topic WINDOW w AS (PARTITION BY help_category_id); -- +---------------+------------------+---------------------------+ -- | help_topic_id | help_category_id | MIN(help_topic_id) OVER w | -- +---------------+------------------+---------------------------+ -- | 0 | 1 | 0 | -- | 1 | 1 | 0 | -- | 2 | 2 | 2 | -- | 6 | 2 | 2 | -- | 7 | 2 | 2 | -- | 8 | 2 | 2 | -- | 9 | 2 | 2 | -- ... -- +---------------+------------------+---------------------------+
Reference information of the MIN() function:
MIN(expr): min Returns the minimum value of expr. Arguments, return value and availability: expr: Required. The field expression in result set groups. min: Return value. The minimum value of the input expression. Available since MySQL 4.
Related MySQL functions:
⇒ STD() - Synonym for STDDEV_POP()
⇐ MAX() - Maximum Value in Group
2023-11-18, 261🔥, 0💬
Popular Posts:
How to connect SQL Server Management Studio Express to SQL Server 2005 Express in SQL Server? Once y...
Where to find answers to frequently asked questions I am new to Oracle database. Here is a list of f...
How To Use SQL*Plus Built-in Timers in Oracle? If you don't have a stopwatch/timer and want to measu...
How To List All User Names in a Database in SQL Server? If you want to see a list of all user names ...
How To Locate and Take Substrings with CHARINDEX() and SUBSTRING() Functions in SQL Server Transact-...