array_difference
Calculates the difference between every two neighboring elements of an array by subtracting each element from its following element and returns an array that consists of the differences.
Syntaxβ
array_difference(input)
Parametersβ
input: the array for which you want to calculate the differences between every two neighboring elements.
Return valueβ
Returns an array with the same length as input. The return element type is determined by the input element type as follows:
| Input element type | Return element type |
|---|---|
BOOLEAN, TINYINT, SMALLINT, INT, or BIGINT | BIGINT |
LARGEINT | LARGEINT |
FLOAT or DOUBLE | DOUBLE |
DECIMAL(P, S), where P <= 38 | DECIMAL(min(P + 1, 38), S) |
ARRAY<DECIMAL256> (P > 38) is not supported.
Examplesβ
Example 1:
mysql> SELECT array_difference([342, 32423, 213, 23432]);
+-----------------------------------------+
| array_difference([342,32423,213,23432]) |
+-----------------------------------------+
| [0,32081,-32210,23219] |
+-----------------------------------------+
Example 2:
mysql> SELECT array_difference([342, 32423, 213, null, 23432]);
+----------------------------------------------+
| array_difference([342,32423,213,NULL,23432]) |
+----------------------------------------------+
| [0,32081,-32210,null,null] |
+----------------------------------------------+
Example 3:
mysql> SELECT array_difference([1.2, 2.3, 3.2, 4324242.55]);
+--------------------------------------------+
| array_difference([1.2,2.3,3.2,4324242.55]) |
+--------------------------------------------+
| [0,1.1,0.9,4324239.35] |
+--------------------------------------------+
Example 4:
mysql> SELECT array_difference([false, true, false]);
+----------------------------------------+
| array_difference([FALSE, TRUE, FALSE]) |
+----------------------------------------+
| [0,1,-1] |
+----------------------------------------+