Wyodrębnianie zagnieżdżonych kluczy JSON
Zespół terenowy chciałby teraz, abyś wyodrębnił bieżącą temperaturę oraz temperaturę o godzinie 4:00. Klucz hour_temperature zawiera tablicę, która zaczyna się od północy (0:00). JSON jest ponownie dostępny w CTE.
To ćwiczenie jest częścią kursu
Wprowadzenie do Redshift
Instrukcje do ćwiczenia
- Wyodrębnij bieżącą temperaturę AS
current_tempz danych stacji pogodowej. - Wyodrębnij
'hourly_temperature'o godzinie 4:00 jakofour_am_tempz danych stacji pogodowej.
Interaktywne ćwiczenie praktyczne
Spróbuj tego ćwiczenia, uzupełniając ten przykładowy kod.
-- weather_station CTE with the JSON in the data column
WITH weather_station AS (
SELECT '
{
"location": "Salmon Challis National Forest",
"date": "2024-02-10",
"weather": "Rainy",
"temperature": {
"current": 10,
"min": 8,
"max": 12,
"hourly_temperature": [8, 8, 9, 9, 10, 10, 11, 11, 12]
}
}'::SUPER::VARCHAR as data
-- Above line casts to SUPER and then to
-- VARCHAR to ensure it's ready for parsing
)
-- Extract the current temperature
SELECT ___(data, ___, ___) AS current_temp,
-- Extract the hourly_temperature at 4AM
___(data, ___,___, ___) as four_am_temp
-- Use the CTE
FROM ___;