16.20.16.20 - 16.20 - Example: Querying a 1-D ARRAY Data Type and Table using ARRAY - Teradata Vantage NewSQL Engine

Teradata Vantageā„¢ Data Types and Literals

prodname
Teradata Database
Teradata Vantage NewSQL Engine
vrm_release
16.20
category
Programming Reference
featnum
B035-1143-162K

Consider the following 1-D ARRAY data type and table.

CREATE TYPE item_price AS DECIMAL(7,2) ARRAY[20];
CREATE TABLE inventory (itemkind INTEGER,
                        regular_price item_price,
                        sale_price_diff item_price);

Assume the following element values for the sale_price_diff and regular_price arrays:

sale_price_diff[1:20] =  100, 200, 120, 140, 50, 160, 45, 10, 90, 100
                         50, 100, 200, 90, 250, 550, 200, 200, 50, 75
regular_price[1:20] =  50, 90, 80, 10, 45, 30, 20, 10, 90, 100, 
                       100, 300, 230, 110, 500, 550, 200, 400, 100, 150

The following query returns a 1-D ARRAY of element type item_price. During evaluation, each element within the specified scope in the regular_price array is combined with the corresponding element of the sale_price_diff array using the arithmetic function ARRAY_SUB.

SELECT ARRAY_SUB(regular_price, sale_price_diff, 10, 20)
FROM inventory;

The query returns a 1-D ARRAY with the following values:

output_array[1:20] = [ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 50, 200, 30, 20, 250, 0, 0, 200, 50, 75]

In the following query, a literal value of 2 is added to all elements in the 1-D ARRAY sale_price_diff.

SELECT ARRAY_ADD(sale_price_diff, 2) FROM inventory;

This query returns a 1-D ARRAY with the following values:

output_array[1:20] = [ 102, 202, 122, 142, 52, 162, 47, 12, 92, 102
                       52, 102, 202, 92, 252, 552, 202, 202, 52, 77]

The following query shows the use of a filtering condition while performing arithmetic operations on an ARRAY. In this example, all elements within the scope [10:20] that have a value less than 1000 are multiplied by 2.

SELECT ARRAY_MUL(regular_price, 2, 10, 20) FROM inventory
WHERE ARRAY_COUNT_DISTINCT(ARRAY_LT(regular_price,1000,10,20),1)>1;

This query returns a 1-D ARRAY with the following values:

output_array[1:20] = [NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 200, 600, 460, 220, 1000, 1100, 400, 800, 200, 300]