leetcode-mysql 2021-05-02
发布日期:2021-05-07 05:14:54 浏览次数:31 分类:精选文章

本文共 2792 字,大约阅读时间需要 9 分钟。

来源:力扣(LeetCode)链接:【侵删】

ExampleA:()

 

国家表:Countries

+---------------+---------+| Column Name   | Type    |+---------------+---------+| country_id    | int     || country_name  | varchar |+---------------+---------+country_id 是这张表的主键。该表的每行有 country_id 和 country_name 两列。

 

天气表:Weather

+---------------+---------+| Column Name   | Type    |+---------------+---------+| country_id    | int     || weather_state | varchar || day           | date    |+---------------+---------+(country_id, day) 是该表的复合主键。该表的每一行记录了某个国家某一天的天气情况。

 写一段 SQL 来找到表中每个国家在 2019 年 11 月的天气类型。

天气类型的定义如下:当 weather_state 的平均值小于或等于15返回 Cold,当 weather_state 的平均值大于或等于 25 返回 Hot,否则返回 Warm。

你可以以任意顺序返回你的查询结果。

查询结果格式如下所示:

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 |+------------+---------------+------------+

 

Result table:+--------------+--------------+| country_name | weather_type |+--------------+--------------+| USA          | Cold         || Austraila    | Cold         || Peru         | Hot          || China        | Warm         || Morocco      | Hot          |+--------------+--------------+

 

解题思路:(case when then else  end)(group by)

select c.country_name,

case when sum(w.weather_state)/count(*)<=15 then 'Cold'

when sum(w.weather_state)/count(*)>=25 then 'Hot'

else 'Warm' end weather_type

from Countries c,Weather w  where c.country_id=w.country_id

and day like '2019-11-%' group by c.country_name;

 

知识点:

case statement

CASE case_value    WHEN when_value THEN statement_list    [WHEN when_value THEN statement_list] ...    [ELSE statement_list]END CASE

or

CASE    WHEN search_condition THEN statement_list    [WHEN search_condition THEN statement_list] ...    [ELSE statement_list]END CASE

 

上一篇:leetcode-mysql 2021-05-03
下一篇:leetcode-mysql 2021-05-01

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年03月24日 18时38分03秒