Example: LOCAL ORDER BY Option - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
ft:locale
en-US
ft:lastEdition
2024-12-11
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

This example uses the same tables as the previous example except for a new version of orders_staging with an integer column named o_sequence, with which to order selected rows locally.

     ALTER TABLE orders_staging
     ADD o_sequence INTEGER;

The following request locally orders rows of columns selected from orders_staging by o_sequence before inserting those rows into orders.

     INSERT INTO orders
       SELECT o_orderkey, o_custkey, o_orderstatus, o_totalprice+10,
              o_ordertsz, o_comment
       FROM orders_staging 
       LOCAL ORDER BY o_sequence;

For the LOCAL ORDER BY clause, o_sequence does not resolve to a column in the target table orders, so the database resolves using the standard rules for resolution to o_sequence in the underlying table of the SELECT request, orders_staging. The orders_staging.o_sequence is not included in the select expression list and the spool is generated locally and sorted on the value of orders_staging.o_sequence before being locally inserting into orders.