17.10 - Example: Using UNPIVOT for a Unique Year Value - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL Functions, Expressions, and Predicates

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Release Date
July 2021
Content Type
Programming Reference
Publication ID
B035-1145-171K
Language
English (United States)

This example shows only one unique value of year, so the unpivot is straightforward.

SELECT *
FROM star1p UNPIVOT (Q1sales, Q2sales, Q3sales, Q1cogs, Q2cogs, Q3cogs) FOR
yr IN ((Q101Sales, Q201Sales, Q301Sales, Q101Cogs, Q201Cogs, Q301Cogs) AS
‘2001’) Tmp;

country  state  yr     Q1sales  Q2sales  Q3sales  Q1cogs  Q2cogs  Q3cogs
-------  -----  ----  --------  -------  -------  ------  ------  ------
Canada   ON     2001    ?         10        ?      ?        0       ?
Canada   BC     2001    ?          ?       10      ?        ?       0
USA      NY     2001    45         ?        ?      25       ?       ?
USA      CA     2001    30        50        ?      15       20