Launch price: every exam for $29.99 a year, for the first 300 buyers. See the price

CertStreak
Menu

Transform data by denormalizing, pivoting, and unpivoting data

A worked example

Multiple answers

The table regional_targets has the columns region, jan, feb, mar and apr, with exactly one row per region. Some monthly target cells are null because those targets have not been set yet. You must reshape the data for a machine learning pipeline. The result must meet all of these requirements:

  1. The output columns are region, month and target_amount, with one row per region per month.
  2. Months whose target is null must still appear as rows.
  3. The month values must be January, February, March and April.

Which two queries meet all the requirements? Each correct answer presents a complete solution. Select two.

Choose 2 answers.
Why

Reshaping wide monthly columns into rows is an unpivot. UNPIVOT drops null values unless INCLUDE NULLS is added, so that keyword is needed to keep months with unset targets. In the clause, the column named before FOR receives the values and the column after FOR receives the source column names, and an alias on each listed column changes the label stored in the name column. Only the two queries that combine all three elements meet every requirement.

Practise this topic until it's automatic.

Ten questions a day, every answer explained, and misses that come back until you beat them.

Start free