ORDER BY Clause
Sorts a query result set on one or more columns or column expressions. Vertica uses the current locale and collation sequence to compare and sort string values.
Vertica projection data is always stored sorted by the ASCII (binary) collating sequence.
Syntax
ORDER BY expression [ ASC | DESC ] [,...]
Parameters
expression |
One of the following:
You cannot use DISTINCT on a collection column if it is also included in the sort order. |
ASC | DESC |
Specifies whether to sort values in ascending or descending order. NULL values are either first or last in the sort order, depending on data type:
|
Examples
The follow example returns all the city and deal size for customer Metamedia, sorted by deal size in descending order.
=> SELECT customer_city, deal_siz FROM customer_dimension WHERE customer_name = 'Metamedia' ORDER BY deal_size DESC; customer_city | deal_size ------------------+----------- El Monte | 4479561 Athens | 3815416 Ventura | 3792937 Peoria | 3227765 Arvada | 2671849 Coral Springs | 2643674 Fontana | 2374465 Rancho Cucamonga | 2214002 Wichita Falls | 2117962 Beaumont | 1898295 Arvada | 1321897 Waco | 1026854 Joliet | 945404 Hartford | 445795 (14 rows)
The following example uses a transform function. It returns an error because the ORDER BY column is not in the window partition.
=> CREATE TABLE t(geom geometry(200), geog geography(200)); => SELECT PolygonPoint(geom) OVER(PARTITION BY geom) AS SEL_0 FROM t ORDER BY geog; ERROR 2521: Cannot specify anything other than user defined transforms and partitioning expressions in the ORDER BY list
The following example, using the same table, corrects this error.
=> SELECT PolygonPoint(geom) OVER(PARTITION BY geom) AS SEL_0 FROM t ORDER BY geom;
The following example uses an array in the ORDER BY clause.
=> CREATE TABLE employees (id INT, department VARCHAR(50), grants ARRAY[VARCHAR], grant_values ARRAY[INT]); => COPY employees FROM STDIN; 42|Physics|[US-7376,DARPA-1567]|[65000,135000] 36|Physics|[US-7376,DARPA-1567]|[10000,25000] 33|Physics|[US-7376]|[30000] 36|Astronomy|[US-7376,DARPA-1567]|[5000,4000] \. => SELECT * FROM employees ORDER BY grant_values; id | department | grants | grant_values ----+------------+--------------------------+---------------- 36 | Astronomy | ["US-7376","DARPA-1567"] | [5000,4000] 36 | Physics | ["US-7376","DARPA-1567"] | [10000,25000] 33 | Physics | ["US-7376"] | [30000] 42 | Physics | ["US-7376","DARPA-1567"] | [65000,135000] (4 rows)