開始使用免費開始

擷取巢狀 JSON 鍵

外勤團隊希望你擷取目前氣溫,以及清晨 4 點的氣溫。他們表示 hour_temperature 鍵包含一個從凌晨 12 點(午夜)開始的陣列。JSON 已放在 CTE 中供你使用。

本練習屬於課程

Redshift 入門

檢視課程

練習說明

  • 從氣象站資料中擷取目前氣溫,並命名為 current_temp
  • 從氣象站資料中擷取清晨 4 點的 'hourly_temperature',並命名為 four_am_temp

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

-- 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 ___;
編輯並執行程式碼