LeetCode-1294. Weather patterns in different countries (simple)

National Table: Countries

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| country_id | int |
| country_name | VARCHAR |
+ --------------- + --------- +
country_id is this Zhang table's primary key.
Each row of the table and has country_id country_name two.
 

Weather Table: Weather

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| country_id | int |
| weather_state | VARCHAR |
| Day | DATE |
+ --------------- + -------- - +
(the country_id, Day) is a composite primary key of the table.
Each row of the table records the weather conditions of a country one day.
 

Write a SQL table for each country to find the weather patterns in the November 2019.

Weather type is defined as follows: when the average is less than or equal to weather_state 15 returns Cold, weather_state when the average is greater than or equal to 25 return Hot, otherwise Warm.

You can return your query results in any order.

Results shown in the following format:

Countries table:
+------------+--------------+
| country_id | country_name |
+------------+--------------+
| 2          | USA          |
| 3          | Australia    |
| 7          | Peru         |
| 5          | China        |
| 8          | Morocco      |
| 9          | Spain        |
+------------+--------------+
Weather table:
+------------+---------------+------------+
| country_id | weather_state | day        |
+------------+---------------+------------+
| 2          | 15            | 2019-11-01 |
| 2          | 12            | 2019-10-28 |
| 2          | 12            | 2019-10-27 |
| 3          | -2            | 2019-11-10 |
| 3          | 0             | 2019-11-11 |
| 3          | 3             | 2019-11-12 |
| 5          | 16            | 2019-11-07 |
| 5          | 18            | 2019-11-09 |
| 5          | 21            | 2019-11-23 |
| 7          | 25            | 2019-11-28 |
| 7          | 22            | 2019-12-01 |
| 7          | 20            | 2019-12-02 |
| 8          | 25            | 2019-11-05 |
| 8          | 27            | 2019-11-15 |
| 8          | 31            | 2019-11-25 |
| 9          | 7             | 2019-10-23 |
| 9          | 3             | 2019-12-23 |
+------------+---------------+------------+
The Table the Result:
+ -------------- + -------------- +
| country_name | weather_type |
+ ---------- + -------------- + ----
| USA | Cold |
| Austraila | Cold |
| Peru | Hot |
| China | Warm |
| Morocco | Hot |
+ ---- -------------- + + ----------
USA November weather_state average is (15) / 1 = 15 so weather type Cold.
Australia weather_state November average is (-2 + 0 + 3) / 3 = 0.333 So the weather type Cold.
Peru in November for the average weather_state (25) / 25 = 1 type of weather it Hot.
China in November weather_state average is (16 + 18 + 21) / 3 = 18.333 So the weather type Warm.
Morocco weather_state November average is (25 + 27 + 31) / 3 = 27.667 So the weather type is Hot.
We do not know weather_state situation in Spain in November so he will not need to be included in the results.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/weather-type-in-each-country
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Moderation: to write a SQL table for each country to find the weather patterns in the November 2019.

Thoughts: convert the date to May, selected in November, averaging, when selected by the weather.

Problem solving:

select country_name, 
case when avg(weather_state)<= 15 then 'Cold'
    when avg(weather_state)>= 25 then 'Hot' 
    else 'Warm'
end as weather_type
from countries c
join weather w 
on c.country_id=w.country_id
where day like'2019-11%'  
group by country_name; 

Method Two:

select c.country_name,
if(avg_wea<= 15,'Cold',if(avg_wea<25,'Warm','Hot')) as weather_type
from 
(select  country_id,avg(weather_state) as avg_wea from Weather
where day between '2019-11-01' and '2019-11-30'
group by country_id) as w
left join Countries c 
on w.country_id = c.country_id

 Knowledge points:

Published 144 original articles · won praise 2 · Views 5736

Guess you like

Origin blog.csdn.net/Hello_JavaScript/article/details/104779005