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)

columntype
diary_idBIGINT
duck_idBIGINT
entry_dateDATE
diary_textVARCHAR
moodVARCHAR

duck_surveys (5000 rows)

columntype
survey_idBIGINT
duck_idBIGINT
ageBIGINT
favorite_foodVARCHAR
favorite_pondBIGINT
swimming_ratingBIGINT
migration_preferenceVARCHAR
social_behaviorVARCHAR
health_statusVARCHAR
activity_levelVARCHAR
survey_dateDATE
survey_locationBIGINT

ducks (15 rows)

columntype
idBIGINT
nameVARCHAR
ageBIGINT
speciesVARCHAR
birth_dateDATE
arrival_dateDATE

ponds (3 rows)

columntype
pond_idBIGINT
pond_nameVARCHAR
location_latDOUBLE
location_lngDOUBLE
capacityBIGINT
featuresVARCHAR[]
amenitiesVARCHAR[]

Files

data/wide_data.csv (4 rows)

columntype
speciesVARCHAR
pond1_visitsBIGINT
pond2_visitsBIGINT
pond3_visitsBIGINT
total_visitsBIGINT

Database: https://dbquacks.com/data/databases/tutorial.duckdb

This page as markdown: https://dbquacks.com/tutorial/32.md

Start level 32 →

DuckDB docs for this lesson