Skip to content

Commit d40e31a

Browse files
author
openset
committed
Add: Weather Type in Each Country
1 parent 3b37d75 commit d40e31a

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

problems/weather-type-in-each-country/README.md

+82
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,86 @@
1111

1212
## [1294. Weather Type in Each Country (Easy)](https://leetcode.com/problems/weather-type-in-each-country "")
1313

14+
<p>Table: <code>Countries</code></p>
15+
<pre>
16+
+---------------+---------+
17+
| Column Name | Type |
18+
+---------------+---------+
19+
| country_id | int |
20+
| country_name | varchar |
21+
+---------------+---------+
22+
country_id is the primary key for this table.
23+
Each row of this table contains the ID and the name of one country.
24+
</pre>
25+
26+
<p>Table: <code>Weather</code></p>
27+
<pre>
28+
+---------------+---------+
29+
| Column Name | Type |
30+
+---------------+---------+
31+
| country_id | int |
32+
| weather_state | varchar |
33+
| day | date |
34+
+---------------+---------+
35+
(country_id, day) is the primary key for this table.
36+
Each row of this table indicates the weather state in a country for one day.
37+
</pre>
38+
39+
Write an SQL query to find the type of weather in each country for November 2019.
1440

41+
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.
42+
43+
Return result table in any order.
44+
45+
The query result format is in the following example:
46+
<pre>
47+
Countries table:
48+
+------------+--------------+
49+
| country_id | country_name |
50+
+------------+--------------+
51+
| 2 | USA |
52+
| 3 | Australia |
53+
| 7 | Peru |
54+
| 5 | China |
55+
| 8 | Morocco |
56+
| 9 | Spain |
57+
+------------+--------------+
58+
Weather table:
59+
+------------+---------------+------------+
60+
| country_id | weather_state | day |
61+
+------------+---------------+------------+
62+
| 2 | 15 | 2019-11-01 |
63+
| 2 | 12 | 2019-10-28 |
64+
| 2 | 12 | 2019-10-27 |
65+
| 3 | -2 | 2019-11-10 |
66+
| 3 | 0 | 2019-11-11 |
67+
| 3 | 3 | 2019-11-12 |
68+
| 5 | 16 | 2019-11-07 |
69+
| 5 | 18 | 2019-11-09 |
70+
| 5 | 21 | 2019-11-23 |
71+
| 7 | 25 | 2019-11-28 |
72+
| 7 | 22 | 2019-12-01 |
73+
| 7 | 20 | 2019-12-02 |
74+
| 8 | 25 | 2019-11-05 |
75+
| 8 | 27 | 2019-11-15 |
76+
| 8 | 31 | 2019-11-25 |
77+
| 9 | 7 | 2019-10-23 |
78+
| 9 | 3 | 2019-12-23 |
79+
+------------+---------------+------------+
80+
Result table:
81+
+--------------+--------------+
82+
| country_name | weather_type |
83+
+--------------+--------------+
84+
| USA | Cold |
85+
| Austraila | Cold |
86+
| Peru | Hot |
87+
| China | Warm |
88+
| Morocco | Hot |
89+
+--------------+--------------+
90+
Average weather_state in USA in November is (15) / 1 = 15 so weather type is Cold.
91+
Average weather_state in Austraila in November is (-2 + 0 + 3) / 3 = 0.333 so weather type is Cold.
92+
Average weather_state in Peru in November is (25) / 1 = 25 so weather type is Hot.
93+
Average weather_state in China in November is (16 + 18 + 21) / 3 = 18.333 so weather type is Warm.
94+
Average weather_state in Morocco in November is (25 + 27 + 31) / 3 = 27.667 so weather type is Hot.
95+
We know nothing about average weather_state in Spain in November so we don't include it in the result table.
96+
</pre>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Write your MySQL query statement below
2+
3+
SELECT country_name,
4+
CASE
5+
WHEN avg(weather_state) <= 15 THEN "Cold"
6+
WHEN avg(weather_state) >= 25 THEN "Hot"
7+
ELSE "Warm" END AS weather_type
8+
FROM Countries
9+
INNER JOIN Weather ON Countries.country_id = Weather.country_id
10+
WHERE LEFT(DAY, 7) = '2019-11'
11+
GROUP BY country_name

0 commit comments

Comments
 (0)