CTESQLStandard
From PostgreSQL wiki
Jump to navigationJump to searchFunction
Specify a table
Format
<query expression>
[ <with clause> ] <query expression body> [ <order by clause> ] [ <result offset clause> ] [ <fetch first clause> ]
<query expression>
[ <with clause> ] <query expression body> [ <order by clause> ] [ <result offset clause> ] [ <fetch first clause> ]
<with list>
<with list element> [ { <comma> <with list element> }... ]
<with list element>
<query name> [ <left paren> <with column list> <right paren> ] AS <table subquery> [ <search or cycle clause> ]
<with column list>
<column name list>
<query expression body>
<query term> | <query expression body> UNION [ ALL | DISTINCT ] [ <corresponding spec> ] <query term> | <query expression body> EXCEPT [ ALL | DISTINCT ] [ <corresponding spec> ] <query term>
<query term>
<query primary> | <query term> INTERSECT [ ALL | DISTINCT ] [ <corresponding spec> ] <query primary>
<query primary>
<simple table> | <left paren> <query expression body> [ <order by clause> ] [ <result offset clause> ] [ <fetch first clause> ] <right paren>
<simple table>
<query specification> | <table value constructor> | <explicit table>
<explicit table>
TABLE <table or query name>
<corresponding spec>
CORRESPONDING [ BY <left paren> <corresponding column list> <right paren> ]
<corresponding column list>
<column name list>
<order by clause>
ORDER BY <sort specification list>
<result offset clause>
OFFSET <offset row count> { ROW | ROWS }
<fetch first clause>
FETCH { FIRST | NEXT } [ <fetch first row count> ] { ROW | ROWS } ONLY
<offset row count>
<simple value specification>
<fetch first row count>
<simple value specification>
Syntax Rules
- Let QE be the <query expression>.
- If <with clause> is specified, then:
- If a <with clause> WC immediately contains RECURSIVE, then WC, its <with list>, and its <with list element>s are said to be potentially recursive. Otherwise they are said to be non-recursive.
- Let n be the number of <with list element>s. For each i , 1 (one) ≤ i < n, for each j, i < j ≤ n, the j-th <with list element> shall not immediately contain a <query name> that is equivalent to the <query name> immediately contained in the i-th <with list element>.
- If the <with clause> is non-recursive, then for all i between 1 (one) and n, the scope of the <query name> WQN immediately contained in the i-th <with list element> WLEi is the <query expression> simply contained in every <with list element> WLEk, where k ranges from i+1 to n, and the <query expression body> immediately contained in <query expression>. A <table or query name> contained in this scope that immediately contains WQN is a query name in scope.
- If the <with clause> is potentially recursive, then for all i between 1 (one) and n, the scope of the <query name> WQN immediately contained in the i-th <with list element> WLEi is the <query expression> simply contained in every <with list element> WLEk, where k ranges from 1 (one) to n, and the <query expression body> immediately contained in <query expression>. A <table or query name> contained in this scope that immediately contains WQN is a query name in scope.
- For every <with list element> WLE, let WQE be the <query expression> specified by WLE and let WQT be the table defined by WQE.
- If any two columns of WQT have equivalent names or if WLE is potentially recursive, then WLE shall specify a <with column list>. If WLE specifies a <with column list> WCL, then:
- Equivalent <column name>s shall not be specified more than once in WCL.
- The number of <column name>s in WCL shall be the same as the degree of WQT.
- Every column of a character string type in WQT shall have a declared type collation.
- A query name dependency graph QNDG of a potentially recursive <with list> WL is a directed graph such that, for i ranging from 1 (one) to the number of <query name>s simply contained in WL:
- Each node represents a <query name> WQNi immediately contained in a <with list element> WLEi of WL.
- Each arc from a node WQNi to a node WQNj represents the fact that WQNj is referenced by a <query name> contained in the <query expression> simply contained in WLEi. WQNi is said to depend immediately on WQNj.
- For a potentially recursive <with list> WL with n elements, and for i ranging from 1 (one) to n, let WLEi be the i-th <with list element> of WL, let WQNi be the <query name> immediately contained in WLEi, let WQEi be the <query expression> simply contained in WLEi, let WQTi be the table defined by WQEi, and let QNDG be the query name dependency graph of WL.
- WL is said to be recursive if QNDG contains at least one cycle. Case:
- If QNDG contains an arc from WQNi to itself, then WLEi, WQNi, and WQTi are said to be recursive. WQNi is said to belong to the stratum of WQEi.
- If QNDG contains an arc from WQNi to itself, then WLEi, WQNi, and WQTi are said to be recursive. WQNi is said to belong to the stratum of WQEi.
- Among the WQEi, ... WQEk of a given stratum, there shall be at least one <query expression>, say WQEj, such that:
- WQEj is a <query expression body> that immediately contains UNION.
- WQEj has one operand that does not contain a <query name> referencing any of WQNi, ..., WQNk. This operand is said to be the non-recursive operand of WQEj.
- WQEj is said to be an anchor expression, and WQNj an anchor name.
- Let CCCG be the subgraph of QNDG that contains no nodes other than WQNi, ..., WQNk. For any anchor name WQNj, remove the arcs to those query names WQNl that are referenced by any <query name> contained in WQEj. The remaining graph SCCGP shall not contain a cycle.
- If WLEi is recursive, then Case:
- If WQEi contains at most one WQNk that belongs to the stratum of WQEi, then WLEi is linearly recursive.
- Otherwise, let WQEi contain any two <query name>s referencing WQNk and WQNl, both of which belong to the stratum of WQEi. Case:
- WLEi is linearly recursive if each of the following conditions is satisfied:
- WQEi does not contain a <table reference list> that contains <query name>s referencing both WQNk and WQNl.
- WQEi does not contain a <joined table> of which TR1 and TR2 are the first and second <table reference>s, or the <table reference> and <table factor>, respectively, and such that TR1 and TR2 contain <query name>s referencing WQNk and WQNl, respectively.
- WQEi does not contain a <table expression> that immediately contains a <from clause> that contains WQNk, and immediately contains a <where clause> containing a <query expression> that contains a <query name> referencing WQNl.
- Otherwise, WLEi is said to be non-linearly recursive.
- For each WLEi, for i ranging from 1 (one) to n, and for each WQNj that belongs to the stratum of WQEi:
- WQEi shall not contain a <query expression body> that contains a <query name> referencing WQNj and immediately contains EXCEPT where the right operand of EXCEPT contains WQNj.
- WQEi shall not contain a <routine invocation> with an <SQL argument list> that contains one or more <SQL argument>s that immediately contain a <value expression> that contains a <query name> referencing WQNj.
- WQEi shall not contain a <query expression> TSQ that contains a <query name> referencing WQNj, unless TSQ is simply contained in a <derived table> that is immediately contained in a <table primary> that is immediately contained in a <table reference> that is immediately contained in a <from clause> that is immediately contained in a <table expression> that is immediately contained in a <query specification> that constitutes a <simple table> that constitutes a <query primary> that constitutes a <query term> that is immediately contained in a <query expression body> that is WQEi.
- WQEi shall not contain a <query specification> QS such that:
- QS immediately contains a <table expression> TE that contains a <query name> referencing WQNj, and
- QS immediately contains a <select list> SL or TE immediately contains a <having clause> HC and SL or TE contain a <set function specification>.
- WQEi shall not contain a <query expression body> that contains a <query name> referencing WQNj and simply contains INTERSECT ALL or EXCEPT ALL.
- WQEi shall not contain a <qualified join> QJ in which:
- QJ immediately contains a <join type> that specifies FULL and a <table reference> or <table factor> that contains a <query name> referencing WQNj.
- QJ immediately contains a <join type> that specifies LEFT and a <table factor> following the <join type> that contains a <query name> referencing WQNj.
- QJ immediately contains a <join type> that specifies RIGHT and a <table reference> preceding the <join type> that contains a <query name> referencing WQNj.
- WQEi shall not contain a <natural join> QJ in which:
- QJ immediately contains a <join type> that specifies FULL and a <table reference> or <table primary> that contains a <query name> referencing WQNj.
- QJ immediately contains a <join type> that specifies LEFT and a <table primary> following the <join type> that contains a <query name> referencing WQNj.
- QJ immediately contains a <join type> that specifies RIGHT and a <table reference> preceding the <join type> that contains a <query name> referencing WQNj. iv) If WLEi is recursive, then WLEi shall be linearly recursive.
- WLEi is said to be expandable if all of the following are true:
- WLEi is recursive.
- WLEi is linearly recursive.
- WQEi is a <query expression body> that immediately contains UNION or UNION ALL. Let WQEBi be the <query expression body> immediately contained in WQEi. Let QELi and QTRi be the <query expression body> and the <query term> immediately contained in WQEBi. WQNi shall not be contained in QELi, and QTRi shall be a <query specification>.
- WQNi is not mutually recursive.
- If a <with list element> WLE is not expandable, then it shall not immediately contain a <search or cycle clause>.