On 14 Dec 2012 14:52, Gary Thompson wrote:
I am curious to know how this [an NTE "Nested Table Expression"]
may compare/contrast with CTE "Common Table Expression"
  They are similar, but the biggest differences AFaIK are that only the 
CTE can be used for recursion, and the NTE both appears in the FROM 
clause [which might be preferred or considered cluttered] and can be 
used directly for correlated references including LATERAL.  An NTE can 
have non-unique column names which avoids naming the columns when they 
are unreferenced outside, but [I believe] a CTE must have a column list 
or have otherwise uniquely named the columns "AS".  The CTE can be 
defined once yet used multiple times in the table-references of the 
later CTEs and the final SELECT, whereas the same temporary intermediate 
result set written as NTEs must be defined multiply.
  Both are scoped only for use within the statement, and the files into 
which the temporary data is stored, are true temporary objects; i.e. no 
ownership\authority costs [overhead] as with permanent objects.  Noting: 
Despite the possible implication [or false inference] per the naming 
"QTEMP", the external object types in QTEMP are "permanent objects" with 
all ownership and authority assigned; foremost, because they can be 
/moved/ into a permanent library.  A reference to a VIEW may be able to 
be used in a query, instead of either a CTE or NTE; the table 
expressions may be necessary or simply deemed better than having to 
create, maintain, and possibly drop a VIEW.  A correlation-name is 
required for a NTE, but a CTE /name/ can be the correlation-name just as 
either a VIEW or TABLE name as table-reference can be used.
  After having written all that... from the documentation, see the "can 
be used":
IBM i 7.1 Information Center -> Database -> Reference -> SQL reference 
-> Queries -> select-statement
http://publib.boulder.ibm.com/infocenter/iseries/v7r1m0/topic/db2/rbafzcomtexp.htm
_i common-table-expression i_
"A common-table-expression permits defining a result table with a 
table-identifier that can be specified as a table name in any FROM 
clause of the fullselect that follows. Multiple common table expressions 
can be specified following the single WITH keyword. Each common table 
expression specified can also be referenced by name in the FROM clause 
of subsequent common table expressions.
...
A common-table-expression can be used:
    * In place of a view to avoid creating the view (when general use 
of the view is not required and positioned UPDATE or DELETE is not used)
    * To enable grouping by a column that is derived from a 
scalar-fullselect or function that is not deterministic
    * When the required result table is based on variables
    * When the same result table needs to be shared in a fullselect
    * When the result needs to be derived using recursion
..."
IBM i 7.1 Information Center -> Database -> Reference -> SQL reference 
-> Queries -> subselect -> from-clause
http://publib.boulder.ibm.com/infocenter/iseries/v7r1m0/topic/db2/rbafztabref.htm
_i table-reference i_
"A table-reference specifies an intermediate result table.
...
A nested table expression can be used:
    * in place of a view to avoid creating the view (when general use 
of the view is not required)
    * when the wanted result table is based on variables.
..."
As an Amazon Associate we earn from qualifying purchases.