advertisement
Window Function Order of Evaluation Aster Data proprietary and confidential
•
• a PARTITION BY clause whose partition_expression is used to group rows into partitions (rows that evaluate equally for this expression are considered a partition); and/or an ORDER BY clause whose order_expression provides the sorting criteria and whose
optional parameters such as ASC further specify the sorting behavior. See “ORDER BY
Clause in SELECT” on page V-80 for details. The default sorting behavior is “ASC
NULLS LAST”.
Window function behavior is defined largely by the PARTITION BY and ORDER BY clauses in the OVER clause, which group and sort the input rows consumed by the window function. A partition is defined as all the rows for which the PARTITION BY expression evaluates to the same value. Each partition is sorted according to the ORDER BY clause (if present). The window function is then computed over each partition, considering each row in order and returning an output value for each row. Because window functions are computed on a per-row basis, the number of rows is not changed by using a window function. The OVER clause does not determine the ordering of output rows.
Important!
Window functions themselves provide no guarantee of the ordering of output
rows. The ORDER BY subclause of the OVER clause does not determine the ordering of output rows. To sort output rows, the query must have a query-wide ORDER BY clause in
addition to those used in any window functions. For details, see “Window Function Example
4: Output Row Ordering” on page V-142 .
Certain subclauses of the OVER clause may be required or optional, depending on the type of window function you are using:
•
•
•
The PARTITION BY clause is optional, but using one is strongly recommended. If no
PARTITION BY clause is present, the entire input relation is considered to be one partition.
See the warning in
ORDER BY without PARTITION BY (page V-156) .
The ORDER BY clause is required if the function is a numbering window function, and
optional otherwise. See Numbering Window Functions (page V-139) .
If your window function is an aggregate window function, then the OVER clause can optionally include a ROWS or RANGE clause that defines a window frame (not shown in
the syntax synopsis above), as explained in “Window Frame Syntax” on page V-147 .
Window Function Order of Evaluation
From the point of view of the person querying the database, window functions are evaluated
after all filtering, grouping, and aggregation is done. This means that a window function can
SELECT clause except another window function. (That is, you may not nest window functions.)
Aster Database-supported window functions may be divided broadly into three categories:
•
•
•
numbering window functions, e.g., RANK(), DENSE_RANK(), and ROW_NUMBER()
aggregate window functions, e.g. AVG(), SUM(), COUNT(). All SQL aggregates supported
in Aster Database may be used as window functions. (See “Aggregate Functions” on page V-130 .)
lead/lag window functions, which take as input an expression evaluated at a specified offset ahead of or behind the current row, respectively.
V--138 Database SQL and Function Reference, version 4.6.2
aster data
advertisement
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
advertisement
Table of contents
- 5 Conventions Used in This Guide
- 5 Typefaces
- 5 SQL Text Conventions
- 6 Command Shell Text Conventions
- 6 Contacting Technical Support
- 6 About Aster Data
- 7 About This Document
- 14 ABORT
- 14 ABORT -- abort the current transaction
- 14 Synopsis
- 14 Description
- 14 Parameters
- 14 Notes
- 14 Examples
- 14 Compatibility
- 14 See Also
- 15 ALTER INDEX
- 15 ALTER INDEX -- change the definition of an index
- 15 Synopsis
- 15 Description
- 15 Parameters
- 15 Notes
- 15 Examples
- 15 Compatibility
- 15 ALTER ROLE
- 15 ALTER ROLE -- change a database role
- 15 Synopsis
- 16 Description
- 16 Parameters
- 16 Notes
- 16 Compatibility
- 16 See Also
- 16 ALTER SCHEMA
- 16 Synopsis
- 16 Description
- 16 Parameters
- 17 See Also
- 17 ALTER TABLE
- 17 ALTER TABLE -- change the definition of a table
- 17 Synopsis
- 18 Description
- 18 Actions for ALTER TABLE
- 21 Parameters for ALTER TABLE
- 21 Notes About ALTER TABLE
- 21 ALTER TABLE Examples
- 23 Compatibility of ALTER TABLE
- 23 ALTER USER
- 23 ALTER USER - changes attributes of a user
- 23 Synopsis
- 23 Example usage
- 23 Description
- 24 Parameters
- 24 Compatibility
- 24 See Also
- 24 ALTER VIEW
- 24 ALTER VIEW -- change the definition of a view
- 24 Synopsis
- 24 Description
- 25 Parameters
- 25 Notes
- 25 Examples
- 25 Compatibility
- 25 See Also
- 25 ANALYZE
- 25 ANALYZE -- collect statistics about a database
- 25 Synopsis
- 25 Description
- 26 Parameters
- 26 Outputs from ANALYZE
- 26 Notes About ANALYZE
- 26 Compatibility
- 26 See Also
- 26 BEGIN
- 26 BEGIN -- start a transaction block
- 26 Synopsis
- 26 Description
- 27 Parameters
- 27 Notes
- 27 Examples
- 27 Compatibility
- 27 See Also
- 28 CASE
- 28 CLOSE
- 28 CLOSE -- close a cursor
- 28 Synopsis
- 28 Description
- 28 Parameters for CLOSE
- 28 Notes
- 28 Examples
- 28 Compatibility
- 28 See Also
- 29 CLUSTER
- 29 Synopsis
- 29 Description
- 29 Parameters
- 29 Notes
- 30 Examples
- 30 Compatibility
- 30 COALESCE
- 30 COMMIT
- 30 COMMIT -- commit the current transaction
- 30 Synopsis
- 30 Description
- 30 Parameters
- 30 Notes
- 30 Examples
- 31 Compatibility
- 31 See Also
- 31 COPY
- 31 COPY -- copy data between a client and a table
- 31 Synopsis
- 31 Copy into Aster Database:
- 32 Copy from Aster Database:
- 32 Description
- 32 Parameters for COPY
- 34 Notes About COPY
- 34 Input Formats for COPY
- 34 Text Formatted Input to COPY
- 35 CSV Formatted Input to COPY
- 35 Example Use of COPY
- 36 Compatibility of COPY
- 36 See Also
- 36 CREATE DATABASE
- 36 CREATE DATABASE -- create a new database
- 36 Synopsis
- 36 Description
- 36 Parameters for CREATE DATABASE
- 37 Notes
- 37 Examples
- 37 Compatibility
- 37 CREATE INDEX
- 37 CREATE INDEX -- define a new index
- 37 Synopsis
- 37 Description
- 38 Parameters
- 38 Notes
- 39 Examples
- 39 Compatibility
- 39 See Also
- 39 CREATE ROLE
- 39 Synopsis
- 39 Description
- 39 Parameters
- 40 Notes
- 40 Examples
- 40 Compatibility
- 40 See Also
- 40 CREATE SCHEMA
- 40 CREATE SCHEMA -- define a new schema
- 40 Synopsis
- 41 Description
- 41 Parameters
- 41 See Also
- 42 CREATE TABLE
- 42 CREATE TABLE -- define a new table
- 42 Synopsis
- 43 Description
- 43 Parameters for CREATE TABLE
- 45 Setting Constraints in CREATE TABLE
- 45 Constraint Syntax for CREATE TABLE
- 47 Notes on CREATE TABLE
- 47 Examples of CREATE TABLE
- 48 Compatibility
- 49 See Also
- 49 A Note on Unique Constraints
- 50 CREATE TABLE AS
- 50 CREATE TABLE AS - define a new table from the results of a query
- 50 Synopsis
- 50 Description
- 51 Parameters
- 51 Compatibility
- 51 See Also
- 51 CREATE USER
- 51 CREATE USER
- 51 Synopsis
- 52 Description
- 52 Parameters
- 52 Notes
- 52 Examples
- 52 Compatibility
- 53 Compatibility
- 53 See Also
- 53 CREATE VIEW
- 53 CREATE VIEW
- 53 Synopsis
- 53 Synopsis
- 53 Description
- 53 Parameters
- 54 Notes
- 54 Example
- 54 Compatibility
- 54 See also
- 54 DECLARE
- 54 DECLARE -- define a cursor
- 54 Synopsis
- 55 Description
- 56 Parameters for DECLARE
- 56 Notes on DECLARE
- 57 Examples
- 57 Compatibility of DECLARE
- 57 See Also
- 57 DELETE
- 57 DELETE -- delete rows of a table
- 57 Synopsis
- 57 Description
- 58 Parameters for DELETE
- 58 Outputs
- 58 Notes on DELETE
- 58 Examples
- 59 Compatibility of DELETE
- 59 DROP DATABASE
- 59 DROP DATABASE -- remove a database
- 59 Synopsis
- 59 Description
- 59 Parameters for DROP DATABASE
- 59 Notes
- 59 Compatibility
- 59 See Also
- 59 DROP INDEX
- 59 DROP INDEX -- remove an index
- 59 Synopsis
- 60 Description
- 60 Parameters for DROP INDEX
- 60 Examples
- 60 Compatibility
- 60 See Also
- 60 DROP ROLE
- 60 DROP ROLE -- remove a role from Aster Database
- 60 Synopsis
- 60 Parameters
- 60 Examples
- 60 Description
- 61 Compatibility
- 61 See Also
- 61 DROP SCHEMA
- 61 Synopsis
- 61 Description
- 61 Parameters
- 61 See Also
- 61 DROP TABLE
- 61 DROP TABLE -- remove a table
- 61 Synopsis
- 62 Description
- 62 Parameters
- 62 Examples
- 62 Compatibility
- 62 See Also
- 62 DROP USER
- 62 DROP USER -- delete a user from Aster Database
- 62 Synopsis
- 62 Example Usage
- 62 Description
- 63 Compatibility
- 63 See Also
- 63 DROP VIEW
- 63 DROP VIEW -- remove a view
- 63 Synopsis
- 63 Description
- 63 Parameters
- 63 Examples
- 63 Compatibility
- 63 See Also
- 64 END
- 64 END -- commit the current transaction
- 64 Synopsis
- 64 Description
- 64 Parameters
- 64 Notes
- 64 Examples
- 64 Compatibility
- 64 See Also
- 65 EXPLAIN
- 65 EXPLAIN -- show the execution plan of a statement
- 65 Synopsis
- 65 Description
- 65 Parameters
- 65 Notes
- 65 Compatibility
- 65 See Also
- 65 FETCH
- 65 FETCH -- retrieve rows from a query using a cursor
- 65 Synopsis
- 66 Description
- 67 Parameters
- 67 Output
- 67 Notes
- 68 Examples
- 68 Compatibility
- 68 See Also
- 69 GRANT
- 69 GRANT -- define access privileges
- 69 Synopsis
- 69 Description
- 69 GRANT on Database Objects
- 71 GRANT on Roles
- 71 Notes on GRANT
- 71 Examples
- 71 Compatibility
- 72 See Also
- 72 INSERT
- 72 INSERT -- create new rows in a table
- 72 Synopsis
- 72 Description
- 72 Parameters
- 72 Outputs
- 73 Notes
- 73 Examples
- 73 Working With Default Values
- 74 Compatibility
- 74 MERGE
- 74 MERGE -- update or insert rows of a table based on source data
- 74 Synopsis
- 74 Description
- 75 Parameters and Clauses
- 75 Output
- 75 Notes
- 76 Example
- 76 Compatibility
- 76 See Also
- 77 MOVE
- 77 MOVE -- position a cursor
- 77 Synopsis
- 77 Description
- 77 Output
- 77 Examples
- 77 Compatibility
- 77 See Also
- 78 REINDEX
- 78 REINDEX -- rebuild indexes
- 78 Synopsis
- 78 Description
- 78 Parameters
- 78 Notes
- 79 Examples
- 79 Compatibility
- 79 REVOKE
- 79 REVOKE -- remove access privileges
- 79 Synopsis
- 79 Description
- 80 Notes
- 81 Examples
- 81 Compatibility
- 81 See Also
- 82 ROLLBACK
- 82 ROLLBACK -- abort the current transaction
- 82 Synopsis
- 82 Description
- 82 Parameters
- 82 Notes
- 82 Examples
- 82 Compatibility
- 82 See Also
- 83 SELECT
- 83 SELECT -- retrieve rows from a table or view
- 83 Synopsis of SELECT
- 84 Description of SELECT
- 84 Clauses of the SELECT Statement
- 85 The SELECT List
- 85 FROM Clause in SELECT
- 86 WHERE Clause in SELECT
- 86 GROUP BY Clause in SELECT
- 87 HAVING Clause in SELECT
- 87 UNION Clause in SELECT
- 87 INTERSECT Clause in SELECT
- 88 EXCEPT Clause in SELECT
- 88 ORDER BY Clause in SELECT
- 89 DISTINCT Clause in SELECT
- 89 LIMIT Clause in SELECT
- 90 Examples of SELECT Statements
- 90 Compatibility of SELECT
- 90 Omitted FROM Clauses
- 90 No Support for VALUES Clause
- 90 Namespace Available to GROUP BY and ORDER BY
- 91 Nonstandard Clauses
- 91 SET
- 91 SET -- Set the value of a runtime configuration parameter
- 91 Synopsis
- 91 Description
- 91 Scope of a Parameter Setting
- 91 Runtime Parameters
- 93 Examples Using SET
- 93 Compatibility
- 93 See Also
- 93 SHOW
- 93 SHOW -- Display value of a run-time configuration parameter
- 93 Synopsis
- 93 Description
- 94 Parameters for SHOW
- 94 Example of SHOW
- 94 Compatibility
- 95 See Also
- 95 START TRANSACTION
- 95 START TRANSACTION -- start a transaction block
- 95 Synopsis
- 95 Description
- 95 Parameters
- 95 Notes
- 95 Example
- 95 Compatibility
- 95 See Also
- 96 TRUNCATE
- 96 TRUNCATE -- empty a table or set of tables
- 96 Synopsis
- 96 Description
- 96 Parameters
- 96 Notes
- 96 Examples
- 96 Compatibility
- 97 See Also
- 97 UPDATE
- 97 UPDATE -- update rows of a table
- 97 Synopsis
- 97 Description
- 98 Parameters
- 98 Outputs
- 98 Notes
- 98 Examples
- 99 Compatibility
- 100 VACUUM
- 100 VACUUM -- garbage-collect and optionally analyze a table (or, if cluster is so configured, a database)
- 100 Synopsis
- 100 Description
- 101 Parameters
- 101 Outputs
- 101 Notes
- 102 Compatibility
- 102 See Also
- 102 WITH
- 102 WITH -- convenience syntax that lets you declare and name a sub-SELECT query
- 102 Synopsis
- 102 Description
- 103 Logical Operators
- 104 Comparison Operators
- 105 Mathematical Operators and Functions
- 107 Trigonometric Functions
- 108 String Functions and Operators
- 108 SQL String Functions and Operators
- 109 Additional String Functions and Operators
- 111 Bit String Functions and Operators
- 111 SQL/MapReduce Functions
- 112 Synopsis
- 112 nPath
- 112 nPath Synopsis
- 113 nPath Pattern
- 113 nPath Symbols
- 113 nPath Operators
- 114 Nesting parentheses in nPath
- 114 nPath Anchors
- 114 nPath Examples
- 114 nPath Example 1: Lead
- 114 nPath Example 2: Rank
- 115 nPath Example 3: Complex Path Query
- 115 nPath Aggregates
- 116 Pattern Matching Functions and Operators
- 116 LIKE
- 117 SIMILAR TO Regular Expressions
- 118 SUBSTRING Function with Three Parameters
- 118 POSIX Regular Expressions
- 119 SUBSTRING Function with Two Parameters
- 119 regexp_replace Function
- 119 regexp_split_to_table Function
- 119 Some regexp_split_to_table Examples
- 120 Regular Expression Details
- 122 Bracket Expressions
- 123 Regular Expression Escapes
- 125 Regular Expression Metasyntax
- 126 Regular Expression Matching Rules
- 128 Limits and Compatibility
- 128 Basic Regular Expressions
- 129 Datatype Formatting Functions and Operators
- 131 Date/Time Functions and Operators
- 131 Date/Time Operators
- 132 Date/Time Functions
- 132 EXTRACT Function
- 133 century Field Name
- 133 day Field Name
- 133 decade Field Name
- 133 dow Field Name
- 133 doy Field Name
- 133 epoch Field Name
- 134 hour Field Name
- 134 microseconds Field Name
- 134 millenium Field Name
- 134 milliseconds Field Name
- 134 minute Field Name
- 134 month Field Name
- 135 quarter Field Name
- 135 second Field Name
- 135 timezone Field Name
- 135 week Field Name
- 135 year Field Name
- 135 date_part Function
- 136 date_trunc Function
- 136 date_trunc Arguments
- 136 date_trunc Examples
- 137 Current Date/Time
- 138 Aggregate Functions
- 138 Aggregate Functions for Statistics
- 139 Conditional SQL Expressions
- 139 CASE
- 140 COALESCE
- 141 NULLIF
- 141 GREATEST and LEAST
- 141 Subquery SQL Expressions
- 141 EXISTS
- 141 IN
- 142 NOT IN
- 142 ANY/SOME
- 143 ALL
- 145 Synopsis of Window Function Syntax
- 146 Window Function Order of Evaluation
- 147 Numbering Window Functions
- 147 ROW_NUMBER()
- 147 RANK()
- 148 DENSE_RANK()
- 148 Numbering Window Function Examples
- 148 Window Function Example 1: ROW_NUMBER()
- 148 Window Function Example 2: RANK()
- 149 Window Function Example 3: DENSE_RANK()
- 150 Window Function Example 4: Output Row Ordering
- 151 Window Function Example 5: Multiple RANK()s
- 151 Window Function Example 6: Attributes
- 152 Window Function Example 7: Expressions, Improper Use
- 152 Window Function Example 8: Expressions, Proper Use
- 153 LEAD and LAG functions
- 153 Syntax
- 153 LEAD/LAG Examples
- 153 Window Function Example 9: LEAD()
- 154 Window Function Example 10: LAG()
- 154 Aggregate Window Functions
- 155 Window Frame Syntax
- 156 Window Frame Requirements
- 156 Valid Examples
- 156 Invalid Examples
- 157 ORDER BY Required
- 157 Window Frame Types: ROWS vs. RANGE
- 157 Window Function Example 11: Running AVG()
- 158 Window Function Example 12: Moving AVG()
- 158 Window Function Example 13: Cumulative Running AVG()
- 159 The Default Window Frame
- 159 Window Function Example 14: Running SUM()
- 160 Window Function Example 15: ORDER BY SUM()
- 160 Window Function Example 16: SUM(SUM(n))
- 161 Consistent Sort Behavior of Input Rows
- 161 Warning: Rewrite When Using Specific Left Endpoint and Unbounded Right Endpoint
- 162 Repartitioning Performance for Window Functions and SQL-MapReduce Queries
- 162 Deprecated Behavior
- 163 Examples of the Old Aliasing Behavior
- 163 Example That Uses an Alias in a Window Function (Deprecated)
- 163 Window Function Known Issues
- 163 COUNT(*)
- 164 ORDER BY without PARTITION BY
- 164 Use in SELECT Clause Only
- 164 No Support for WINDOW Clause
- 164 Size Limit of Sliding Window Frames
- 164 Limited Support for RANGE-Based Window Frames
- 165 List of Supported Datatypes
- 165 List of Types
- 166 Compatibility
- 167 Numeric Types
- 167 Integer Types
- 168 Arbitrary Precision Numbers
- 169 Floating-Point Types
- 170 Serial Types
- 170 Local and Global
- 170 Creating Columns of Type Serial
- 170 Automatic Numbering
- 171 Primary Key Constraint Not Recommended on Serial Columns
- 171 Actual Datatype of a Serial Column
- 171 Character Types
- 172 Storage Requirements
- 172 Tip for Using Character Types
- 172 Examples
- 173 Date/Time Types
- 173 Date/Time Input
- 174 Date Input Formats
- 174 Time Input Formats
- 175 Time Stamp Input Formats
- 176 Intervals
- 176 Date/Time Output
- 177 Time Zones
- 177 Bit String Types
- 177 Boolean Types
- 177 Allowed Boolean Values
- 178 Examples
- 178 Binary Types
- 179 Entering bytea Values
- 179 bytea Output Representations
- 180 Compatibility
- 180 Network Address Types
- 180 ip4 and ip4range Datatypes in Aster Database
- 180 Simple usage examples
- 181 ip4 Datatype
- 181 Type Conversions
- 182 Operators and Functions
- 182 ip4range Datatype
- 183 Typecasting for ip4range
- 183 Functions for ip4range
- 183 Operators for ip4range
- 184 GiST Indexes (ip4range Indexes)
- 184 Use Cases for ip4range
- 185 Examples Using ip4 and ip4range
- 185 Example: GiST indexes
- 185 Example: Joins on IP addresses and ranges
- 185 Examples: IP address-related functions and operators
- 186 UUID Type
- 187 Type Casts
- 189 Date/Time Input Interpretation
- 190 Date/Time Keywords
- 190 Month Names
- 190 Day-of-Week Names
- 191 Date Time Field Modifiers
- 194 Introduction to Data Dictionary Views
- 194 User-Related Data Dictionary Views
- 195 Role-Related Data Dictionary Views
- 195 Group Membership Data Dictionary Views
- 195 Database-Related Data Dictionary Views
- 196 Schema-Related Data Dictionary Views
- 196 SQL-MapReduce and Installed File-Related Data Dictionary Views
- 196 nc_user_installed_files, nc_user_owned_installed_files, nc_all_ installed_files
- 197 nc_user_sqlmr_funcs, nc_user_owned_sqlmr_funcs, nc_all_sqlmr_ funcs
- 197 nc_user_sqlmr_func_privs, nc_user_owned_sqlmr_func_privs, nc_ all_sqlmr_func_privs
- 198 Table-Related Data Dictionary Views
- 198 Column-Related Data Dictionary Views
- 199 Index-Related Data Dictionary Views
- 199 Constraint-Related Data Dictionary Views
- 200 Logical Partition-Related Data Dictionary Views
- 201 Inheritance-Related Data Dictionary Views
- 201 Types Data Dictionary View
- 201 Cluster State Data Dictionary Views
- 201 Physical Node State: nc_physical_node_state
- 202 Storage State: nc_cluster_storage
- 202 Activity Data Dictionary Views
- 202 Session Statistics: nc_all_sessions
- 203 Transaction Statistics: nc_all_transactions
- 203 Transaction Phases: nc_all_transaction_phases
- 203 Statement Statistics: nc_all_statements
- 204 Load Error Logging Tables
- 204 Schema of the Load Error Logging Tables
- 204 Sample Entries in a Load Error Logging Table
- 205 Creating a Load Error Logging Table
- 205 Load Error Statistics Tables
- 206 Temporary Data Dictionary Views
- 207 Identifiers, Keywords, and Naming Conventions
- 208 Quoted Identifiers
- 209 Comments in SQL
- 209 Value Expressions
- 210 Column References
- 219 A
- 219 B
- 219 C
- 220 D
- 221 E
- 221 F
- 222 G
- 222 H
- 222 I
- 222 J
- 223 K
- 223 L
- 223 M
- 223 N
- 224 O
- 224 P
- 225 Q
- 225 R
- 225 S
- 227 T
- 227 U
- 228 V
- 228 W
- 228 X
- 228 Y
- 228 Symbols