Reshape a flat list of values into n rows of equal-width tuples.
STACK(n, expr1, expr2, ...)
## Overview Reshapes a flat list of expressions into a table with `n` rows. The expressions are consumed left to right: the first `total / n` expressions form row 1, the next group forms row 2, and so on. Use STACK to build inline reference tables, to manually unpivot a small set of values, or to materialize test data without touching a base table. ## Behavior - The total number of value expressions must be evenly divisible by n. - Column types are inferred by position from the corresponding values in the first row. - Values in the same column must be type-compatible. - Deterministic for a given set of arguments. - Side effect free. ## Compatibility - Standard analytical SQL primitive for inline row materialization. Commonly used in ad-hoc unpivot workflows.
| Name | Type | Description |
|---|---|---|
n | Specifies the number of rows to produce. The total number of value expressions must be evenly divisible by n. | |
expr | Specifies the values to distribute across rows. Expressions are consumed left to right, filling each row before advancing to the next. |
-- Reshape 4 values into 2 rows of 2 columns
SELECT * FROM STACK(2, 'a', 1, 'b', 2);
-- Reshape 6 values into 3 rows of 2 columns
SELECT * FROM STACK(3, 'x', 10, 'y', 20, 'z', 30);
-- Reshape 3 values into 3 rows of 1 column
SELECT * FROM STACK(3, 'red', 'green', 'blue');
-- Unpivot fixed values for a simple summary
SELECT metric, value FROM STACK(3, 'revenue', 1000, 'cost', 750, 'profit', 250);
-- Build an inline lookup table
SELECT code, label FROM STACK(2, 'A', 'Active', 'I', 'Inactive');