Issue Details (XML | Word | Printable)

Key: CORE-1688
Type: New Feature New Feature
Status: Reopened Reopened
Priority: Minor Minor
Assignee: Adriano dos Santos Fernandes
Reporter: Philippe Makowski
Votes: 13
Watchers: 8
Operations

If you were logged in you would be able to see more operations.
Firebird Core

ANSI SQL2003 Window Functions

Created: 12/Jan/08 12:34 PM   Updated: 03/May/13 10:37 AM
Component/s: Engine
Affects Version/s: None
Fix Version/s: None

Time Tracking:
Issue & Sub-Tasks
Issue Only
Not Specified

Issue Links:
Replace
 

Sub-Tasks  All   Open   

 Description  « Hide
After CTE, it would be nice to have ANSI SQL Window Functions

SQL2003 allows for a window_clause in aggregate function calls, the addition of which makes those functions into window functions.
SQL2003 specifies the following syntax for window functions:

FUNCTION_NAME(expr) OVER {window_name|(window_specification)}
window_specification ::= [window_name][partitioning][ordering][framing]
partitioning ::= PARTITION BY value [, value...] [COLLATE collation_name]
ordering ::= ORDER [SIBLINGS] BY rule [, rule...]
rule ::= {value|position|alias} [ASC|DESC] [NULLS {FIRST|LAST}]
framing ::= {ROWS|RANGE} {start|between} [exclusion]
start ::= {UNBOUNDED PRECEDING|unsigned-integer PRECEDING|CURRENT ROW}
between ::= BETWEEN bound AND bound
bound ::= {start|UNBOUNDED FOLLOWING|unsigned-integer FOLLOWING}
exclusion ::= {EXCLUDE CURRENT ROW|EXCLUDE GROUP
              |EXCLUDE TIES|EXCLUDE NO OTHERS}


List of Window Functions
CUME_DIST( ) OVER {window_name|(window_specification)} : Calculates the cumulative distribution, or relative rank, of the current row to other rows in the same partition

DENSE_RANK( ) OVER {window_name|(window_specification)} : Assigns a rank to each row in a partition, which should be ordered in some manner. The rank for a given row is computed by counting the number of rows preceding the row in question, and then adding 1 to the result. Rows with duplicate ORDER BY values will rank the same. Unlike the case with RANK( ), gaps in rank numbers will not result from two rows sharing the same rank.

RANK( ) OVER {window_name|(window_specification)} : Assigns a rank to each row in a partition, which should be ordered in some manner. The rank for a given row is computed by counting the number of rows preceding the row in question, and then adding 1 to the result. Rows with duplicate ORDER BY values will rank the same, and will lead to subsequent gaps in rank numbers.

PERCENT_RANK( ) OVER ({window_name|(window_specification)} : Computes the relative rank of a row by dividing that row's rank less 1 by the number of rows in the partition, also less 1

ROW_NUMBER( ) OVER ({window_name|(window_specification)} : Assigns a unique number to each row in a partition.

 All   Comments   Work Log   Change History   Version Control   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Volker Rehn added a comment - 18/Aug/08 02:46 PM
Adriano has asked what to implement first since Philippe's list is fairly big. Please see this link on how it is done in Postgres

http://umitanuki.net/pgsql/wfv03/design.html

and this one for a good introduction and summary on this complex topic

http://wwwdvs.informatik.uni-kl.de/courses/NEDM/WS0607/Vorlesungsunterlagen/NEDM.Chapter.06.Windows_and_Query_Functions_in_SQL.pdf

this ticket deserves a higher priority. Even if only partially implemented those features would significantly enhance the toolkit of OLAP users of Firebird.

Philippe Makowski added a comment - 18/Aug/08 04:01 PM
In my opinion, the priority is to set Window functions (RANK(), ROW_NUMBER())

Adriano dos Santos Fernandes added a comment - 18/Aug/08 04:51 PM
My intention is to implement early a way for current GROUP BY functions work with OVER ([PARTITION BY ...]) clause. Later, some internal refactoring would be desired to make easy creation of new functions.

Functions as RANK and ROW_NUMBER also requires ORDER BY clause, so they probably will not be the first ones.

Volker Rehn added a comment - 17/Sep/08 03:27 AM
the current window function postgres link is

http://umitanuki.net/pgsql/wfv05/design.html

Adriano dos Santos Fernandes added a comment - 05/Apr/11 09:32 PM
I'm resolving this because there is no immediate plan to implement more subtasks.

Philippe Makowski added a comment - 03/Oct/11 04:30 PM - edited
Adriano, did you implemented :

percent_rank() relative rank of the current row: (rank - 1) / (total rows - 1)

cume_dist() relative rank of the current row: (number of rows preceding or peer with current row) / (total rows)

ntile(num_buckets integer) integer ranging from 1 to the argument value, dividing the partition as equally as possible

lag(value any [, offset integer [, default any ]]) returns value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead return default. Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null

lead(value any [, offset integer [, default any ]]) returns value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead return default. Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null

first_value(value any) return value evaluated at the row that is the first row of the window frame

last_value(value any) returns value evaluated at the row that is the last row of the window frame

nth_value(value any, nth integer) returns value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row

for ref : http://www.postgresql.org/docs/9.1/static/functions-window.html#FUNCTIONS-WINDOW-TABLE

Adriano dos Santos Fernandes added a comment - 03/Oct/11 04:39 PM
Philippe, of these only LAG and LEAD are already implemented.

Philippe Makowski added a comment - 03/Oct/11 04:50 PM
ok so I reopen this one
and please add sub tasks for
percent_rank()
cume_dist()
ntile(num_buckets integer)
first_value(value any)
last_value(value any)
nth_value(value any, nth integer)


Sean Leyne added a comment - 06/Oct/11 03:28 PM
Sub-tasks created

Philippe Makowski added a comment - 12/Oct/11 03:30 PM
seems that :
framing ::= {ROWS|RANGE} {start|between} [exclusion]
start ::= {UNBOUNDED PRECEDING|unsigned-integer PRECEDING|CURRENT ROW}
between ::= BETWEEN bound AND bound
bound ::= {start|UNBOUNDED FOLLOWING|unsigned-integer FOLLOWING}
exclusion ::= {EXCLUDE CURRENT ROW|EXCLUDE GROUP
              |EXCLUDE TIES|EXCLUDE NO OTHERS}

are not implemeted ?

Dmitry Yemanov added a comment - 13/Oct/11 07:10 AM
Philippe, there was no indication that the whole specification would be implemented at once :-) I recall a kind of agreement with Adriano that framing could be postponed.

Philippe Makowski added a comment - 13/Oct/11 07:20 AM
framing could be postponed, but it have to be indicated somewhere or another ticket or sub-ticket have to be set for that, just to be clear about what we have or not

Philippe Makowski added a comment - 17/Oct/11 02:01 PM
Standard say :
OVER {window_name|(window_specification)}
window_specification ::= [window_name][partitioning][ordering][framing]

so, according to the standard, you can write :
select emp_no, dept_no, salary, avg(salary) over (partition by dept_no)
 from employee;

or you can use an explicit WINDOW clause

select emp_no, dept_no, salary, avg(salary) over w
 from employee
WINDOW w as (partition by dept_no);

but now it returns :
Statement failed, SQLSTATE = 42000
Dynamic SQL Error
-SQL error code = -104
-Token unknown - line 1, column 50
-w


advantage of explicit WINDOW clause : window has a name, which can be used by multiple window table function
invocations in the SELECT clause