UNPIVOT Clause

Description

The UNPIVOT clause transforms multiple columns into multiple rows used in SELECT clause. The UNPIVOT clause can be specified after the table name or subquery.

Syntax

  1. UNPIVOT [ { INCLUDE | EXCLUDE } NULLS ] (
  2. { single_value_column_unpivot | multi_value_column_unpivot }
  3. ) [[AS] alias]
  4. single_value_column_unpivot:
  5. values_column
  6. FOR name_column
  7. IN (unpivot_column [[AS] alias] [, ...])
  8. multi_value_column_unpivot:
  9. (values_column [, ...])
  10. FOR name_column
  11. IN ((unpivot_column [, ...]) [[AS] alias] [, ...])

Parameters

  • unpivot_column

    Contains columns in the FROM clause, which specifies the columns we want to unpivot.

  • name_column

    The name for the column that holds the names of the unpivoted columns.

  • values_column

    The name for the column that holds the values of the unpivoted columns.

Examples

  1. CREATE TABLE sales_quarterly (year INT, q1 INT, q2 INT, q3 INT, q4 INT);
  2. INSERT INTO sales_quarterly VALUES
  3. (2020, null, 1000, 2000, 2500),
  4. (2021, 2250, 3200, 4200, 5900),
  5. (2022, 4200, 3100, null, null);
  6. -- column names are used as unpivot columns
  7. SELECT * FROM sales_quarterly
  8. UNPIVOT (
  9. sales FOR quarter IN (q1, q2, q3, q4)
  10. );
  11. +------+---------+-------+
  12. | year | quarter | sales |
  13. +------+---------+-------+
  14. | 2020 | q2 | 1000 |
  15. | 2020 | q3 | 2000 |
  16. | 2020 | q4 | 2500 |
  17. | 2021 | q1 | 2250 |
  18. | 2021 | q2 | 3200 |
  19. | 2021 | q3 | 4200 |
  20. | 2021 | q4 | 5900 |
  21. | 2022 | q1 | 4200 |
  22. | 2022 | q2 | 3100 |
  23. +------+---------+-------+
  24. -- NULL values are excluded by default, they can be included
  25. -- unpivot columns can be alias
  26. -- unpivot result can be referenced via its alias
  27. SELECT up.* FROM sales_quarterly
  28. UNPIVOT INCLUDE NULLS (
  29. sales FOR quarter IN (q1 AS Q1, q2 AS Q2, q3 AS Q3, q4 AS Q4)
  30. ) AS up;
  31. +------+---------+-------+
  32. | year | quarter | sales |
  33. +------+---------+-------+
  34. | 2020 | Q1 | NULL |
  35. | 2020 | Q2 | 1000 |
  36. | 2020 | Q3 | 2000 |
  37. | 2020 | Q4 | 2500 |
  38. | 2021 | Q1 | 2250 |
  39. | 2021 | Q2 | 3200 |
  40. | 2021 | Q3 | 4200 |
  41. | 2021 | Q4 | 5900 |
  42. | 2022 | Q1 | 4200 |
  43. | 2022 | Q2 | 3100 |
  44. | 2022 | Q3 | NULL |
  45. | 2022 | Q4 | NULL |
  46. +------+---------+-------+
  47. -- multiple value columns can be unpivoted per row
  48. SELECT * FROM sales_quarterly
  49. UNPIVOT EXCLUDE NULLS (
  50. (first_quarter, second_quarter)
  51. FOR half_of_the_year IN (
  52. (q1, q2) AS H1,
  53. (q3, q4) AS H2
  54. )
  55. );
  56. +------+------------------+---------------+----------------+
  57. | id | half_of_the_year | first_quarter | second_quarter |
  58. +------+------------------+---------------+----------------+
  59. | 2020 | H1 | NULL | 1000 |
  60. | 2020 | H2 | 2000 | 2500 |
  61. | 2021 | H1 | 2250 | 3200 |
  62. | 2021 | H2 | 4200 | 5900 |
  63. | 2022 | H1 | 4200 | 3100 |
  64. +------+------------------+---------------+----------------+