©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQL

MySQL ANY_VALUE SQL Function - Value return without GROUP BY specification

ANY_VALUESyntax:ANY_VALUE(Column)
Return value:Depending on the data types used
Function type:Aggregate function
Function Description

The ANY_VALUE() function returns ​​for GROUP BY queries one of its values for a table "Column" if the SQL mode ONLY_FULL_GROUP_BY is enabled and the "Column" is not present in the GROUP BY clause and would otherwise result in an error message.

The function is not an aggregate function and cannot be used in a GROUP BY clause.


This function does not exist in MariaDB.

SQL Examples for the ANY_VALUE function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

DROP TABLE IF EXISTS `test_table`;
    
CREATE TABLE `test_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `type` enum('First','Second','Third','Last'DEFAULT NULL,
  `set` set('First','Second','Third','Last'DEFAULT NULL,
  `num1` int(11) NOT NULL DEFAULT 0,
  `num2` int(11) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

INSERT INTO `test_table` (`id`,`name`,`type`,`set`,`num1`,`num2`VALUES 
(1,'Art 1','First','Last',1,7),
(2,'Art 2','Second','First,Last',3,7),
(3,'Art 3','Second','Second,Last',5,7),
(4,'Art 4','Third','First,Second,Last',5,7),
(5,'Art 5','Last','First,Last',7,15),
(6,'Art 6','First','First,Last',7,15),
(7,'Art 7','Third','First,Third,Last',9,15),
(8,'Art 8','Last','Second,Third,Last',9,15);

select * from `test_table`;

select id, max(num2), `type` from test_table group by `type`;

select any_value(id), max(num2), `type` from test_table group by `type`;

id
name
type
set
num1
num2
intvarchar(20) LATIN1enum LATIN1set LATIN1intint
1Art 1FirstLast17
2Art 2SecondFirst,Last37
3Art 3SecondSecond,Last57
4Art 4ThirdFirst,Second,Last57
5Art 5LastFirst,Last715
6Art 6FirstFirst,Last715
7Art 7ThirdFirst,Third,Last915
8Art 8LastSecond,Third,Last915
   select id, max(num2), `type` from test_table group by `type` 
errno: 1055  sqlstate: 42000  error: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.test_table.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by  
any_value(id)
max(num2)
type

bigint(11)

int(11)
test_table
enum LATIN1
115First
27Second
415Third
515Last
The examples were created with the MyWAY SQL manager: Download

How to use the ANY_VALUE() function in MySQL databases

In MySQL the ANY_VALUE() function is used to indicate that an unclustered column in a SQL query was selected where the query uses the GROUP BY clause. When the GROUP BY clause is used, all selected columns of the result set must either be part of the grouping criteria or used in aggregate functions such as COUNT(), SUM(), AVG(), MAX(), MIN() etc... The function can be used in situations where you want to include a non-clustered column in a SELECT statement without applying an aggregate function to it. Any value from this column is acceptable for each group. The function essentially suppresses the error message when non-aggregated table columns are not part of the GROUP BY clause.

The ANY_VALUE() function is available in MySQL 8.0 and later. Since the return value of the function is undefined, instead of ANY_VALUE() for example functions such as MIN() or MAX can be used as an alternative.
ADDTIMEAES_DECRYPTAES_ENCRYPTANY_VALUEASCIIASINATAN

Further MySQL und MariaDB SQL Aggregate functions

AVGAVG(Expression or column)
More about AVG Function

BIT_ANDBIT_AND(Expression or column)
More about BIT_AND Function

BIT_ORBIT_OR(Expression or column)
More about BIT_OR Function

BIT_XORBIT_XOR(Expression or column)
More about BIT_XOR Function

COUNTCOUNT(Expression or column)
More about COUNT Function

GROUP_CONCATGROUP_CONCAT([DISTINCT] Expression [, Expression ...] [ORDER BY ... [ASC | DESC]] [SEPARATOR ...] [LIMIT ...])
More about GROUP_CONCAT Function

JSON_ARRAYAGGJSON_ARRAYAGG(Expression or column)

JSON_OBJECTAGGJSON_OBJECTAGG(Expression or column)

MAXMAX(Expression or column)
More about MAX Function

MINMIN(Expression or column)
More about MIN Function

SUMSUM(Expression or column)
More about SUM Function

STDSTD(Expression or column)
More about STD Function

STDDEVSTDDEV(Expression or column)
More about STDDEV Function

STDDEV_POPSTDDEV_POP(Expression or column)
More about STDDEV_POP Function

STDDEV_SAMPSTDDEV_SAMP(Expression or column)
More about STDDEV_SAMP Function

VAR_POPVAR_POP(Expression or column)
More about VAR_POP Function

VAR_SAMPVAR_SAMP(Expression or column)
More about VAR_SAMP Function

VARIANCEVARIANCE(Expression or column)
More about VARIANCE Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the ANY_VALUE SQL function: mysql.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer