The unpivot operation works much the same as the SQL concept: "rotating" a set of column headers (and the associated values) into rows to create a more normalized table that's longer and narrower. It is nearly the opposite of the pivot operation, though it will not be able to unmerge records and restore nulls that were aggregated by the pivot operation. You can perform unpivot operations using /aggregate/unpivot or via the SQL UNPIVOT function. Visit the Aggregation documentation to review details on aggregating data.
An unpivot operation could be used to convert a table like this:
|
|
into a table like this:
|
|
Each original record will become three records in the unpivoted version, one for each phone type & number associated with the name. Note that nulls in the source data will still result in an output record being produced; e.g., a contact with no cell phone number will still have a record in the unpivoted output attributing a null phone number with the cell phone type to that contact.
The results of an unpivot may be written to a separate memory-only table using the result_table option. This table can be persisted (like normal tables) using the result_table_persist option.
A memory-only table name must adhere to the standard naming criteria. Each memory-only table exists within a schema and follows the standard name resolution rules for tables.
Examples
Customer
Given a source table customer_contact, which lists the name, ID, and home, work, & cell phone numbers for each customer in the table:
|
|
An unpivot operation can be performed in Python like so, creating separate home, work, & cell phone records for each customer without copying over their names:
|
|
The resulting table looks like this
|
|
Student
Given a source table student_grade, which lists each student's quarterly grades as separate columns in a single record:
|
|
Performing an unpivot operation in Python on the table converts it into a table with separate records for each quarterly grade:
|
|
The resulting table looks like this:
|
|
Limitations
- Store-only columns cannot have unpivot operations applied to them, nor can they appear in the result set
- Non-charN string column types cannot appear in the result set if the result_table option is used
- The column types for the pivoted_columns must match, e.g., int8 to int8, char4 to char4, etc.
Unpivot Memory-Only Tables
- Creating a memory-only table results in an entirely new data set, so be mindful of the memory usage implications.
- An unpivot memory-only table cannot be created with a non-charN string column type.
- If an unpivot memory-only table is created and its source data set's rows are updated (or removed), the memory-only table will not be updated to reflect the changes in the source data set.
- An unpivot memory-only table is transient, by default, and will expire after the default TTL setting.
- An unpivot memory-only table is not persisted, by default, and will not survive a database restart; specifying a result_table_persist option of true will make the table permanent and not expire.