Trích xuất các khóa JSON lồng nhau
Nhóm hiện trường muốn bạn trích xuất nhiệt độ hiện tại và nhiệt độ lúc 4 giờ sáng. Họ cho biết khóa hour_temperature chứa một mảng bắt đầu từ 12 giờ đêm. JSON đã có sẵn trong CTE cho bạn.
Bài tập này là một phần của khóa học
Giới thiệu về Redshift
Hướng dẫn bài tập
- Trích xuất nhiệt độ hiện tại AS
current_temptừ dữ liệu trạm thời tiết. - Trích xuất
'hourly_temperature'lúc 4 giờ sáng ASfour_am_temptừ dữ liệu trạm thời tiết.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
-- 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 ___;