Pivoting Data

Data Types and Functions in Snowflake

Jake Roach

Field Data Engineer

Aggregated table

                   course_name  |  exam_type  |  avg_exam_score
                  ------------- | ----------- | ----------------
                    Calculus I  |  Exam 1     |       81.78
                    Calculus I  |  Exam 2     |       83.55
                    Calculus I  |  Final      |       80.93

                    Finance     |  Exam 1     |       89.47
                    Finance     |  Exam 2     |       90.39
                    Finance     |  Final      |       89.69

                    Marketing   |  Exam 1     |       94.11
                    Marketing   |  Exam 2     |       93.29
                    Marketing   |  Final      |       93.81
Data Types and Functions in Snowflake

"Pivoted" table




         course_name  |    "Exam 1"   |    "Exam 2"   |  "Final"
        ------------- | ------------- | ------------- | ---------
          Calculus I  |     81.78     |     83.55     |   80.93
          Finance     |     89.47     |     90.39     |   89.69
          Marketing   |     94.11     |     93.29     |   93.81



Data Types and Functions in Snowflake

Creating a pivoted table

SELECT
    *
FROM SCHEMA.TABLE

PIVOT(
-- Aggregation function SUM(<1>)
-- Specify rows to pivot to columns FOR <2> IN (ANY ORDER BY <2>) -- No need to GROUP BY! );

PIVOT offers a different way to output aggregated data by "pivoting" values into columns

  • SELECT *, can use EXCLUDE
  • PIVOT goes after FROM ...
  • ANY values in <2>

$$

<1>: field to aggregate

<2>: row values to turn into columns

Data Types and Functions in Snowflake

CTE's and pivoted data

WITH exam_grades AS (
    SELECT
        ..., exam_score, exam_type
    FROM SCHEMA.TABLE
    WHERE ...
)

SELECT * -- Could also use EXCLUDE FROM exam_grades PIVOT( AVG(exam_score) FOR exam_type IN (ANY ORDER BY exam_type) );

First, define a CTE before using PIVOT!

$$

             SELECT * FROM <cte>

$$

$$

$$

exam_score: field to aggregate

exam_type: row values to turn into columns

Data Types and Functions in Snowflake

Comparing exam grades by type

WITH exam_grades AS (
    SELECT 
        course_name, course_abbreviation, exam_score, exam_type
    FROM STUDENTS.grades
    WHERE course_level = '101'
)

SELECT * EXCLUDE course_abbreviation -- Remove course_abbrevation from the result set FROM exam_grades
PIVOT( AVG(exam_score) FOR exam_type IN (ANY ORDER BY exam_type) );
Data Types and Functions in Snowflake

Comparing exam grades by type

                 course_name  |   exam_type   |  avg_exam_score
                ------------- | ------------- | ----------------
                  Calculus I  |   Exam 1      |       81.78
                  Calculus I  |   Exam 2      |       83.55
                  Calculus I  |   Final       |       80.93

                  Finance     |   Midterm 1   |       89.47
                                     ...
                 course_name  |    "Exam 1 "  |    "Exam 2"   |  "Final"
                ------------- | ------------- | ------------- | ---------
                  Calculus I  |     81.78     |     83.55     |   80.93
                  Finance     |     89.47     |     90.39     |   89.69
                  Marketing   |     94.11     |     93.29     |   93.81
Data Types and Functions in Snowflake

Let's practice!

Data Types and Functions in Snowflake

Preparing Video For Download...