ANY_VALUE

An aggregate function that arbitrarily picks one value from the group. This can include a NULL value if one is present in the group.

It is a non-deterministic function (returns different results each time they are called with a specific set of input values).

May be used as a window function.

Syntax

Aggregate Function

ANY_VALUE (expression);

Window Function

ANY_VALUE([DISTINCT] (col | expr ) OVER (
[PARTITION BY (col | expr), ...]
[ORDER BY (col | expr), ...] ]
)

Arguments

  • expression: any numerical or string expression

Return Type

Returns the same value and type as its argument.

Examples

CREATE TABLE av_employees(
emp_id NUMERIC(5),
emp_lastname VARCHAR(25),
emp_firstname VARCHAR(25),
emp_title VARCHAR(25),
dept VARCHAR(25),
emp_city VARCHAR(25),
emp_state VARCHAR(2)
);
INSERT INTO av_employees VALUES('014', 'Bateman', 'Patrick','Prod_Mgr', 'prod_dev', 'NYC', 'NY'),
('102', 'Karras', 'Damien', 'Doctor','R&D', 'NYC', 'NY'),
('298', 'Denbrough', 'Bill', 'Salesperson','Sales', 'Bangor', 'ME'),
('399', 'Torrance', 'Jack', 'PR Dir', 'PR','Estes Park','CO'),
('410', 'Wilkes', 'Annie', 'HR Mgr','HR','Silver Creek', 'CO'),
('110', 'Strode', 'Laurie', 'VP Sales','Sales', 'Haddonfield', 'IL'),
('312', 'Cady', 'Max', 'IT Dir', 'IT', 'New Essex','FL'),
('089', 'Whateley', 'Wilbur', 'CEO', 'Sen_Mgmt', 'Dunwich', 'MA'),
('075', 'White', 'Carrie', 'Receptionist', 'HR','Chamberlain', 'ME'),
('263', 'MacNeil', 'Regan', 'R&D Mgr','R&D', 'Washington', 'DC');
SELECT * FROM av_employees;
+--------+--------------+---------------+--------------+----------+--------------+-----------+
| emp_id | emp_lastname | emp_firstname | emp_title    | dept     | emp_city     | emp_state |
+--------+--------------+---------------+--------------+----------+--------------+-----------+
|    102 | Karras       | Damien        | Doctor       | R&D      | NYC          | NY        |
|    410 | Wilkes       | Annie         | HR Mgr       | HR       | Silver Creek | CO        |
|     14 | Bateman      | Patrick       | Prod_Mgr     | prod_dev | NYC          | NY        |
|    110 | Strode       | Laurie        | VP Sales     | Sales    | Haddonfield  | IL        |
|    298 | Denbrough    | Bill          | Salesperson  | Sales    | Bangor       | ME        |
|     89 | Whateley     | Wilbur        | CEO          | Sen_Mgmt | Dunwich      | MA        |
|     75 | White        | Carrie        | Receptionist | HR       | Chamberlain  | ME        |
|    399 | Torrance     | Jack          | PR Dir       | PR       | Estes Park   | CO        |
|    312 | Cady         | Max           | IT Dir       | IT       | New Essex    | FL        |
|    263 | MacNeil      | Regan         | R&D Mgr      | R&D      | Washington   | DC        |
+--------+--------------+---------------+--------------+----------+--------------+-----------+

The following example displays the use of ANY_VALUE aggregate function with the GROUP BY statement.

Aggregate Function

SELECT ANY_VALUE(emp_lastname), emp_city FROM av_employees GROUP BY emp_city;
+-------------------------+--------------+
| ANY_VALUE(emp_lastname) | emp_city     |
+-------------------------+--------------+
| Strode                  | Haddonfield  |
| MacNeil                 | Washington   |
| White                   | Chamberlain  |
| Torrance                | Estes Park   |
| Denbrough               | Bangor       |
| Bateman                 | NYC          |
| Cady                    | New Essex    |
| Wilkes                  | Silver Creek |
| Whateley                | Dunwich      |
+-------------------------+--------------+

Window Function

SELECT emp_id, any_value(emp_id) OVER (ORDER BY emp_id) FROM av_employees;
+--------+------------------------------------------+
| emp_id | any_value(emp_id) OVER (ORDER BY emp_id) |
+--------+------------------------------------------+
|     14 |                                       14 |
|     75 |                                       14 |
|     89 |                                       14 |
|    102 |                                       14 |
|    110 |                                       14 |
|    263 |                                       14 |
|    298 |                                       14 |
|    312 |                                       14 |
|    399 |                                       14 |
|    410 |                                       14 |
+--------+------------------------------------------+

Last modified: September 6, 2024

Was this article helpful?

Verification instructions

Note: You must install cosign to verify the authenticity of the SingleStore file.

Use the following steps to verify the authenticity of singlestoredb-server, singlestoredb-toolbox, singlestoredb-studio, and singlestore-client SingleStore files that have been downloaded.

You may perform the following steps on any computer that can run cosign, such as the main deployment host of the cluster.

  1. (Optional) Run the following command to view the associated signature files.

    curl undefined
  2. Download the signature file from the SingleStore release server.

    • Option 1: Click the Download Signature button next to the SingleStore file.

    • Option 2: Copy and paste the following URL into the address bar of your browser and save the signature file.

    • Option 3: Run the following command to download the signature file.

      curl -O undefined
  3. After the signature file has been downloaded, run the following command to verify the authenticity of the SingleStore file.

    echo -n undefined |
    cosign verify-blob --certificate-oidc-issuer https://oidc.eks.us-east-1.amazonaws.com/id/CCDCDBA1379A5596AB5B2E46DCA385BC \
    --certificate-identity https://kubernetes.io/namespaces/freya-production/serviceaccounts/job-worker \
    --bundle undefined \
    --new-bundle-format -
    Verified OK