Coalesce
De functie coalesce() is handig om een standaard- of terugvalwaarde op te geven wanneer een kolom NULL-waarden bevat.
coalesce() controleert de argumenten op volgorde en geeft de eerste niet-NULL waarde terug, als die bestaat.
coalesce(NULL, 1, 2)= 1coalesce(NULL, NULL)=NULLcoalesce(2, 3, NULL)= 2
In de fortune500-gegevens bevat industry enkele missende waarden. Gebruik coalesce() om de waarde van sector als industrie te gebruiken wanneer industry NULL is. Zoek daarna de meest voorkomende industrie.
Deze oefening maakt deel uit van de cursus
Exploratory Data Analysis in SQL
Oefeninstructies
- Gebruik
coalesce()om de eerste niet-NULLwaarde te selecteren uitindustry,sectorof, als terugval,'Unknown'. - Geef het resultaat van de aanroep van
coalesce()de aliasindustry2. - Tel het aantal rijen met elke
industry2-waarde. - Zoek de meest voorkomende waarde van
industry2.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
-- Use coalesce
SELECT ___(___, ___, 'Unknown') AS industry2,
-- Don't forget to count!
___
FROM ___
-- Group by what? (What are you counting by?)
GROUP BY ___
-- Order results to see most common first
___ ___ ___ ___
-- Limit results to get just the one value you want
___ ___;