Views:
TypeRead/writeAuthorAvailability
Direct providerReadFinbourneProvided with LUSID

The Tools.Unpivot provider enables you to write a Luminesce query that rotates data from columns into rows to accomplish the same types of transformations as the SQL Server and Snowflake UNPIVOT functions. 

Note: The LUSID user running the query must have sufficient access control permissions to use this provider. This should automatically be the case if you are the domain owner.

You can use this provider in conjunction with other providers to unpivot data, see example 3.

See also: Tools.Pivot

Basic usage

@input = select * from <some-provider> where <filter-expression>;

@unpivoted =
use Tools.Unpivot with @input
--<optional-arguments>
enduse;

select * from @unpivoted;

Input tables

Tools.Unpivot takes in one input table and outputs a table of data, see example 1.

Options

Tools.Unpivot has options that enable you to refine a query.

An option takes the form --<option>=<value>, for example --key=LusidInstrumentId. Note no spaces are allowed either side of the = operator. If an option takes a boolean value, then specifying that option (for example --keyIsNotUnique) sets it to True; omitting the option specifies False.

The table below provides information on available options for Tools.Unpivot:

OptionsValueStatusInformation
--key<column_name> [, <column_name>...]MandatoryThe column names that should be preserved in rows (other columns will become values). Normally these would make up the unique key of the input data.
--keyIsNotUniqueBooleanOptionalColumn names specified in --key normally make up the unique key of the input data. Specifying this argument asserts this not to be the case.  
--includeNullsBooleanOptionalSpecifies whether null cells should still produce rows.

To see a help screen of available options, their data types, default values, and an explanation for each, run the following query using a suitable tool:

@x = use Tools.Unpivot
--help
enduse;
select * from @x

Examples

Example 1: Unpivoting basic input data

In this example, we input some simple data...

...before unpivoting that data using column1 and column2 as our key. 

@input = values
    ('a', 'a', 'AA', 1, #2022-02-01#, true, 1.1),
    ('a', 'b', 'BB', 2, #2022-02-02#, false, null),
    ('b', 'a', 'CC', 3, #2022-02-03#, true, 1.3),
    ('b', 'b', 'DD', 4, #2022-02-04#, false, 1.4);

@unpivoted = 
use Tools.Unpivot with @input
    --key=column1, column2
enduse;
select * from @unpivoted


The table of data returned after unpivoting shows column1 and column2 preserved as rows, while the other columns have become values:

Example 2: Unpivoting data with a non-unique key

In this example, we create an input table which looks like this...

...and unpivot the data using column1 and column2 as our key. Notice that the key (highlighted in red) is not unique in this example, so we pass in the argument --keyIsNotUnique to prevent the query from failing. 

@input = values
    ('a', 'a', 'AA', 1, #2022-02-01#, true, 1.1),
    ('a', 'a', 'BB', 2, #2022-02-02#, false, 1.11)
    ('b', 'b', 'DD', 4, #2022-02-04#, false, 1.4);
    
@unpivoted = 
use Tools.Unpivot with @input
--key=column1, column2
--keyIsNotUnique
enduse;

select * from @unpivoted


The table of data returned by the query looks like this:

Example 3: Pivoting a reconciliation response to show metrics in separate columns

You can take a Lusid.CutLabel response that looks like this...

...and run it through the Tools.Unpivot provider to return rows for the key column, TimeZone.

@input = select * from Lusid.CutLabel;

@unpivoted = 
use Tools.Unpivot with @input
    --key=TimeZone
    --keyIsNotUnique
enduse;

select * from @unpivoted 


The table of data returned by the query looks like this, with the TimeZone column preserved as rows while the other columns have become values: