Session Ready
Exercise

Turning rows into columns

In this lesson, you learned that PIVOT turns the unique values from one column into multiple columns.

Analyzing the data of paper_shop_monthly_sales, you realize the structure of this table is not appropriate for the report that you want to make.

You want to generate a report with this appearance:

|year_of_sale|notebooks|pencils|crayons|
|------------|---------|-------|-------|
| 2018       | 150     | 150   | 80    |
| 2019       | 230     | 130   | 170   |

In other words, you want to change the data you have in the rows to data into columns, and sum the units for every year.

As you learned from the previous exercises, the name of the products and the units has to be split. This is done in the subselect, take a look at it.

Instructions
100 XP
  • Select the pivoted columns for every product.
  • Include the sum of the units inside the PIVOT operator.
  • After the FOR statement, include the name of the column that contains the values that will become column headers.
  • Give to the PIVOT operator the name paper_shop_pivot.