Skip to content

Commit cd3d45d

Browse files
add 1294
1 parent 3b2c2ea commit cd3d45d

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ _If you like this project, please leave me a star._ ★
847847
| # | Title | Solutions | Video | Difficulty | Tag
848848
|-----|----------------|---------------|---------------|---------------|-------------
849849
|1327|[List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period/)|[Solution](../master/database/_1327.sql) || Easy |
850+
|1294|[Weather Type in Each Country](https://leetcode.com/problems/weather-type-in-each-country/)|[Solution](../master/database/_1294.sql) | | Easy |
850851
|1280|[Students and Examinations](https://leetcode.com/problems/students-and-examinations/)|[Solution](../master/database/_1280.sql) | [:tv:](https://www.youtube.com/watch?v=ThbkV4Fs7iE)| Easy |
851852
|1251|[Average Selling Price](https://leetcode.com/problems/average-selling-price/)|[Solution](../master/database/_1251.sql) | | Easy |
852853
|1241|[Number of Comments per Post](https://leetcode.com/problems/number-of-comments-per-post/)|[Solution](../master/database/_1241.sql) | | Easy |

database/_1294.sql

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)