开始使用免费开始使用

提取嵌套的 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 ___;
编辑并运行代码