CodeBlanks Logo Icon CodeBlanks

Summary - Pivot

Write a monthly sales pivot with CASE.

Summary - Pivot

Pivot turns rows into columns. Done with CASE and SUM, FILTER is more readable.

SELECT
  SUM(CASE WHEN category = 'Kitap' THEN amount ELSE 0 END) AS Kitap
FROM sales;

Summary pivot query.

SUM(CASE WHEN category = 'Kitap' THEN amount ELSE 0 END) … is selected from sales.

Practice Summary - Pivot

Summary: pivot with SUM(CASE WHEN category = 'Kitap' THEN amount ELSE 0 END).

Starter code

query.sql
-- summary pivot
SELECT
  SUM(

Sample solution

query.sql
SELECT
  SUM(CASE WHEN category = 'Kitap' THEN amount ELSE 0 END) AS Kitap
FROM sales;

Expected output

Kitap
200

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does pivot convert?

  • Rows → columns Correct answer
  • Columns → rows
  • Index → table
  • Table → index

Explanation: Pivot converts rows to columns.

2 Summary - Pivot: match the terms

Term Meaning
pivot row→column
CASE condition
FILTER readable
unpivot reverse

All lessons in Advanced