|
| 1 | +--1294. Weather Type in Each Country |
| 2 | +-- |
| 3 | +--Table: Countries |
| 4 | +-- |
| 5 | +--+---------------+---------+ |
| 6 | +--| Column Name | Type | |
| 7 | +--+---------------+---------+ |
| 8 | +--| country_id | int | |
| 9 | +--| country_name | varchar | |
| 10 | +--+---------------+---------+ |
| 11 | +--country_id is the primary key for this table. |
| 12 | +--Each row of this table contains the ID and the name of one country. |
| 13 | +-- |
| 14 | +-- |
| 15 | +--Table: Weather |
| 16 | +-- |
| 17 | +--+---------------+---------+ |
| 18 | +--| Column Name | Type | |
| 19 | +--+---------------+---------+ |
| 20 | +--| country_id | int | |
| 21 | +--| weather_state | varchar | |
| 22 | +--| day | date | |
| 23 | +--+---------------+---------+ |
| 24 | +--(country_id, day) is the primary key for this table. |
| 25 | +--Each row of this table indicates the weather state in a country for one day. |
| 26 | +-- |
| 27 | +-- |
| 28 | +--Write an SQL query to find the type of weather in each country for November 2019. |
| 29 | +-- |
| 30 | +--The type of weather is Cold if the average weather_state is less than or equal 15, Hot if the average weather_state is greater than or equal 25 and Warm otherwise. |
| 31 | +-- |
| 32 | +--Return result table in any order. |
| 33 | +-- |
| 34 | +--The query result format is in the following example: |
| 35 | +-- |
| 36 | +--Countries table: |
| 37 | +--+------------+--------------+ |
| 38 | +--| country_id | country_name | |
| 39 | +--+------------+--------------+ |
| 40 | +--| 2 | USA | |
| 41 | +--| 3 | Australia | |
| 42 | +--| 7 | Peru | |
| 43 | +--| 5 | China | |
| 44 | +--| 8 | Morocco | |
| 45 | +--| 9 | Spain | |
| 46 | +--+------------+--------------+ |
| 47 | +--Weather table: |
| 48 | +--+------------+---------------+------------+ |
| 49 | +--| country_id | weather_state | day | |
| 50 | +--+------------+---------------+------------+ |
| 51 | +--| 2 | 15 | 2019-11-01 | |
| 52 | +--| 2 | 12 | 2019-10-28 | |
| 53 | +--| 2 | 12 | 2019-10-27 | |
| 54 | +--| 3 | -2 | 2019-11-10 | |
| 55 | +--| 3 | 0 | 2019-11-11 | |
| 56 | +--| 3 | 3 | 2019-11-12 | |
| 57 | +--| 5 | 16 | 2019-11-07 | |
| 58 | +--| 5 | 18 | 2019-11-09 | |
| 59 | +--| 5 | 21 | 2019-11-23 | |
| 60 | +--| 7 | 25 | 2019-11-28 | |
| 61 | +--| 7 | 22 | 2019-12-01 | |
| 62 | +--| 7 | 20 | 2019-12-02 | |
| 63 | +--| 8 | 25 | 2019-11-05 | |
| 64 | +--| 8 | 27 | 2019-11-15 | |
| 65 | +--| 8 | 31 | 2019-11-25 | |
| 66 | +--| 9 | 7 | 2019-10-23 | |
| 67 | +--| 9 | 3 | 2019-12-23 | |
| 68 | +--+------------+---------------+------------+ |
| 69 | +--Result table: |
| 70 | +--+--------------+--------------+ |
| 71 | +--| country_name | weather_type | |
| 72 | +--+--------------+--------------+ |
| 73 | +--| USA | Cold | |
| 74 | +--| Austraila | Cold | |
| 75 | +--| Peru | Hot | |
| 76 | +--| China | Warm | |
| 77 | +--| Morocco | Hot | |
| 78 | +--+--------------+--------------+ |
| 79 | +--Average weather_state in USA in November is (15) / 1 = 15 so weather type is Cold. |
| 80 | +--Average weather_state in Austraila in November is (-2 + 0 + 3) / 3 = 0.333 so weather type is Cold. |
| 81 | +--Average weather_state in Peru in November is (25) / 1 = 25 so weather type is Hot. |
| 82 | +--Average weather_state in China in November is (16 + 18 + 21) / 3 = 18.333 so weather type is Warm. |
| 83 | +--Average weather_state in Morocco in November is (25 + 27 + 31) / 3 = 27.667 so weather type is Hot. |
| 84 | +--We know nothing about average weather_state in Spain in November so we don't include it in the result table. |
| 85 | + |
| 86 | +--# Write your MySQL query statement below |
| 87 | +select c.country_name as country_name, |
| 88 | +case |
| 89 | +when avg(weather_state) <= 15 then "Cold" |
| 90 | +when avg(weather_state) >= 25 then "Hot" |
| 91 | +else "Warm" |
| 92 | +end |
| 93 | +as weather_type |
| 94 | +from Countries c |
| 95 | +join Weather w |
| 96 | +on c.country_id = w.country_id |
| 97 | +and w.day between "2019-11-01" and "2019-11-30" |
| 98 | +group by c.country_id |
0 commit comments