DB Quacks SQL Tutorial · Chapter 6
Transformers: UNPIVOT in Disguise
Optimus Prime Duck transforms with mechanical sounds. Autobots, roll out! Data transformation emergency! The Decepticons created wide dashboard reports, but we need tall analytical format. Use UNPIVOT - reverse of Agent Smiths work!
Your task
Transform a wide pivot table back to tall format with pond names as rows instead of columns using the wide_data.csv file. You will be converting columns back to rows.
Hint
Look for a statement that can convert columns into rows, creating a name-value pair structure.
Expected result
A tall format table with pond visit columns as rows and visit counts as values.
Tables
duck_diaries (10 rows)
| column | type |
|---|---|
diary_id | BIGINT |
duck_id | BIGINT |
entry_date | DATE |
diary_text | VARCHAR |
mood | VARCHAR |
duck_surveys (5000 rows)
| column | type |
|---|---|
survey_id | BIGINT |
duck_id | BIGINT |
age | BIGINT |
favorite_food | VARCHAR |
favorite_pond | BIGINT |
swimming_rating | BIGINT |
migration_preference | VARCHAR |
social_behavior | VARCHAR |
health_status | VARCHAR |
activity_level | VARCHAR |
survey_date | DATE |
survey_location | BIGINT |
ducks (15 rows)
| column | type |
|---|---|
id | BIGINT |
name | VARCHAR |
age | BIGINT |
species | VARCHAR |
birth_date | DATE |
arrival_date | DATE |
ponds (3 rows)
| column | type |
|---|---|
pond_id | BIGINT |
pond_name | VARCHAR |
location_lat | DOUBLE |
location_lng | DOUBLE |
capacity | BIGINT |
features | VARCHAR[] |
amenities | VARCHAR[] |
Files
data/wide_data.csv (4 rows)
| column | type |
|---|---|
species | VARCHAR |
pond1_visits | BIGINT |
pond2_visits | BIGINT |
pond3_visits | BIGINT |
total_visits | BIGINT |
Database: https://dbquacks.com/data/databases/tutorial.duckdb
This page as markdown: https://dbquacks.com/tutorial/32.md