IniziaInizia gratis

Estrazione di chiavi JSON annidate

Il team sul campo ora vuole che tu estragga la temperatura attuale e la temperatura alle 4 del mattino. Hanno detto che la chiave hour_temperature contiene un array che parte dalle 12AM (mezzanotte). Il JSON è di nuovo nella CTE per te.

Questo esercizio fa parte del corso

Introduzione a Redshift

Visualizza il corso

Istruzioni dell'esercizio

  • Estrai la temperatura attuale COME current_temp dai dati della stazione meteo.
  • Estrai la 'hourly_temperature' alle 4 del mattino come four_am_temp dai dati della stazione meteo.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

-- 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 ___;
Modifica ed esegui il codice