Convert Columns Into Rows

UNPIVOT converts a table’s columns into rows.

Create Table Of Superheroes

-- Create a table called SUPERHEROES.
CREATE OR REPLACE TABLE SUPERHEROES (
  -- Column called ALTER_EGO allowing up to 100 characters
  "ALTER_EGO" VARCHAR(100),
  -- Column called BANK_BALANCE of integers
  "AGE" INT,
  -- Column called 2015 of integers
  "2015" INT,
  -- Column called 2016 of integers
  "2016" INT,
  -- Column called 2017 of integers
  "2017" INT,
  -- Column called 2018 of integers
  "2018" INT,
  -- Column called 2019 of integers
  "2019" INT
);

Insert Rows For Each Superhero

-- Insert rows into SUPERHEROES
INSERT INTO SUPERHEROES 
    -- With the values
    VALUES
    ('The Bomber', 45, 1, 5, 7, 8, 0),
    ('Mr. Money', 12, 1, 5, 7, 8, 2),
    ('Nuke Miner', 5, 3, 5, 5, 6, 2),
    ('The Knife', 43, 1, 2, 9, 8, 0),
    ('Ninka Baker', 32, 1, 5, 3, 8, 1),
    ('Banana Bomber', 34, 1, 3, 2, 8, 2),
    ('Augustine', 12, 1, 5, 7, 8, 0),
    ('The Kid', 21, 1, 5, 7, 3, 3),
    ('The Viking', 291, 2, 3, 7, 8, 0);

Convert Years Columns Into Rows

-- Select all columns from SUPERHEROES
SELECT * FROM SUPERHEROES
-- Convert the columns "2015", "2016", "2017", "2018", "2019" into
-- rows with appropriate values in a new a YEARS column 
UNPIVOT(YEAR FOR YEARS IN ("2015", "2016", "2017", "2018", "2019"));
ALTER_EGO AGE YEAR KILLS
The Bomber 45 2015 1
The Bomber 45 2016 5
The Bomber 45 2017 7
The Bomber 45 2018 8
The Bomber 45 2019 0
Mr. Money 12 2015 1
Mr. Money 12 2016 5
Mr. Money 12 2017 7
Mr. Money 12 2018 8
Mr. Money 12 2019 2
Nuke Miner 5 2015 3
Nuke Miner 5 2016 5
Nuke Miner 5 2017 5
Nuke Miner 5 2018 6
Nuke Miner 5 2019 2
The Knife 43 2015 1
The Knife 43 2016 2
The Knife 43 2017 9
The Knife 43 2018 8
The Knife 43 2019 0
Ninka Baker 32 2015 1
Ninka Baker 32 2016 5
Ninka Baker 32 2017 3
Ninka Baker 32 2018 8
Ninka Baker 32 2019 1
Banana Bomber 34 2015 1
Banana Bomber 34 2016 3
Banana Bomber 34 2017 2
Banana Bomber 34 2018 8
Banana Bomber 34 2019 2
Augustine 12 2015 1
Augustine 12 2016 5
Augustine 12 2017 7
Augustine 12 2018 8
Augustine 12 2019 0
The Kid 21 2015 1
The Kid 21 2016 5
The Kid 21 2017 7
The Kid 21 2018 3
The Kid 21 2019 3
The Viking 291 2015 2
The Viking 291 2016 3
The Viking 291 2017 7
The Viking 291 2018 8
The Viking 291 2019 0