เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การดึงข้อมูล JSON key ที่ซ้อนกัน

ทีมภาคสนามต้องการดึงอุณหภูมิปัจจุบันและอุณหภูมิเวลา 4 โมงเช้า โดยระบุว่า key hour_temperature มี array ที่เริ่มต้นที่เวลา 12AM หรือเที่ยงคืน JSON ถูกเตรียมไว้ใน CTE ให้แล้ว

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Introduction to Redshift

ดูคอร์ส

คำแนะนำการฝึกหัด

  • ดึงอุณหภูมิปัจจุบัน AS current_temp จากข้อมูลสถานีตรวจวัดอากาศ
  • ดึงค่า 'hourly_temperature' เวลา 4AM เป็น 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 ___;
แก้ไขและรันโค้ด