【力扣 | SQL题 | 每日三题】力扣1148, 1327, 1211, 1174

devtools/2024/10/4 7:20:44/

1. 力扣1148:文章浏览1

1.1 题目:

Views 表:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| article_id    | int     |
| author_id     | int     |
| viewer_id     | int     |
| view_date     | date    |
+---------------+---------+
此表可能会存在重复行。(换句话说,在 SQL 中这个表没有主键)
此表的每一行都表示某人在某天浏览了某位作者的某篇文章。
请注意,同一人的 author_id 和 viewer_id 是相同的。

请查询出所有浏览过自己文章的作者

结果按照 id 升序排列。

查询结果的格式如下所示:

示例 1:

输入:
Views 表:
+------------+-----------+-----------+------------+
| article_id | author_id | viewer_id | view_date  |
+------------+-----------+-----------+------------+
| 1          | 3         | 5         | 2019-08-01 |
| 1          | 3         | 6         | 2019-08-02 |
| 2          | 7         | 7         | 2019-08-01 |
| 2          | 7         | 6         | 2019-08-02 |
| 4          | 7         | 1         | 2019-07-22 |
| 3          | 4         | 4         | 2019-07-21 |
| 3          | 4         | 4         | 2019-07-21 |
+------------+-----------+-----------+------------+输出:
+------+
| id   |
+------+
| 4    |
| 7    |
+------+

1.2 思路:

where过滤并用distinct去重。

1.3 题解:

sql">select distinct author_id id
from Views
where author_id = viewer_id
order by author_id

2. 力扣1327:列出指定时间段内所有的下单产品

2.1 题目:

表: Products

+------------------+---------+
| Column Name      | Type    |
+------------------+---------+
| product_id       | int     |
| product_name     | varchar |
| product_category | varchar |
+------------------+---------+
product_id 是该表主键(具有唯一值的列)。
该表包含该公司产品的数据。

表: Orders

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| product_id    | int     |
| order_date    | date    |
| unit          | int     |
+---------------+---------+
该表可能包含重复行。
product_id 是表单 Products 的外键(reference 列)。
unit 是在日期 order_date 内下单产品的数目。

写一个解决方案,要求获取在 2020 年 2 月份下单的数量不少于 100 的产品的名字和数目。

返回结果表单的 顺序无要求 

查询结果的格式如下。

示例 1:

输入:
Products 表:
+-------------+-----------------------+------------------+
| product_id  | product_name          | product_category |
+-------------+-----------------------+------------------+
| 1           | Leetcode Solutions    | Book             |
| 2           | Jewels of Stringology | Book             |
| 3           | HP                    | Laptop           |
| 4           | Lenovo                | Laptop           |
| 5           | Leetcode Kit          | T-shirt          |
+-------------+-----------------------+------------------+
Orders 表:
+--------------+--------------+----------+
| product_id   | order_date   | unit     |
+--------------+--------------+----------+
| 1            | 2020-02-05   | 60       |
| 1            | 2020-02-10   | 70       |
| 2            | 2020-01-18   | 30       |
| 2            | 2020-02-11   | 80       |
| 3            | 2020-02-17   | 2        |
| 3            | 2020-02-24   | 3        |
| 4            | 2020-03-01   | 20       |
| 4            | 2020-03-04   | 30       |
| 4            | 2020-03-04   | 60       |
| 5            | 2020-02-25   | 50       |
| 5            | 2020-02-27   | 50       |
| 5            | 2020-03-01   | 50       |
+--------------+--------------+----------+
输出:
+--------------------+---------+
| product_name       | unit    |
+--------------------+---------+
| Leetcode Solutions | 130     |
| Leetcode Kit       | 100     |
+--------------------+---------+
解释:
2020 年 2 月份下单 product_id = 1 的产品的数目总和为 (60 + 70) = 130 。
2020 年 2 月份下单 product_id = 2 的产品的数目总和为 80 。
2020 年 2 月份下单 product_id = 3 的产品的数目总和为 (2 + 3) = 5 。
2020 年 2 月份 product_id = 4 的产品并没有下单。
2020 年 2 月份下单 product_id = 5 的产品的数目总和为 (50 + 50) = 100 。

2.2 思路:

先过滤不是在2020 年 2 月份下单的记录。

然后group by分组,having过滤。

2.3 题解:

sql">select product_name, sum(unit) unit
from Products p 
join (select product_id, order_date, unitfrom Orderswhere order_date <= '2020-02-29' and order_date >= '2020-02-01'
) o 
on p.product_id = o.product_id
group by p.product_name
having sum(unit) >= 100

3. 力扣1211:查询结果的质量和占比

3.1 题目:

Queries 表: 

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| query_name  | varchar |
| result      | varchar |
| position    | int     |
| rating      | int     |
+-------------+---------+
此表可能有重复的行。
此表包含了一些从数据库中收集的查询信息。
“位置”(position)列的值为 1500 。
“评分”(rating)列的值为 15 。评分小于 3 的查询被定义为质量很差的查询。

将查询结果的质量 quality 定义为:

各查询结果的评分与其位置之间比率的平均值。

将劣质查询百分比 poor_query_percentage 定义为:

评分小于 3 的查询结果占全部查询结果的百分比。

编写解决方案,找出每次的 query_name 、 quality 和 poor_query_percentage

quality 和 poor_query_percentage 都应 四舍五入到小数点后两位 。

以 任意顺序 返回结果表。

结果格式如下所示:

示例 1:

输入:
Queries table:
+------------+-------------------+----------+--------+
| query_name | result            | position | rating |
+------------+-------------------+----------+--------+
| Dog        | Golden Retriever  | 1        | 5      |
| Dog        | German Shepherd   | 2        | 5      |
| Dog        | Mule              | 200      | 1      |
| Cat        | Shirazi           | 5        | 2      |
| Cat        | Siamese           | 3        | 3      |
| Cat        | Sphynx            | 7        | 4      |
+------------+-------------------+----------+--------+
输出:
+------------+---------+-----------------------+
| query_name | quality | poor_query_percentage |
+------------+---------+-----------------------+
| Dog        | 2.50    | 33.33                 |
| Cat        | 0.66    | 33.33                 |
+------------+---------+-----------------------+
解释:
Dog 查询结果的质量为 ((5 / 1) + (5 / 2) + (1 / 200)) / 3 = 2.50
Dog 查询结果的劣质查询百分比为 (1 / 3) * 100 = 33.33Cat 查询结果的质量为 ((2 / 5) + (3 / 3) + (4 / 7)) / 3 = 0.66
Cat 查询结果的劣质查询百分比为 (1 / 3) * 100 = 33.33

3.2 思路:

分别用子查询查到

quality | poor_query_percentage 

并添加distinct去重。

至于为什么where需要判断query_name不为空,因为最后一组案例出现了这种特殊情况.

3.3 题解:

sql">select distinct query_name, round((select avg(rating / position)from Queries q1where q.query_name = q1.query_name)
, 2) quality, round((select count(*) / (select count(*) from Queries q4 where q.query_name = q4.query_name) * 100from Queries q2where q.query_name = q2.query_nameand rating <= 2), 2
) poor_query_percentage 
from Queries q 
where query_name is not null
group by query_name

4. 力扣1174:即时食物配送

4.1 题目:

配送表: Delivery

+-----------------------------+---------+
| Column Name                 | Type    |
+-----------------------------+---------+
| delivery_id                 | int     |
| customer_id                 | int     |
| order_date                  | date    |
| customer_pref_delivery_date | date    |
+-----------------------------+---------+
delivery_id 是该表中具有唯一值的列。
该表保存着顾客的食物配送信息,顾客在某个日期下了订单,并指定了一个期望的配送日期(和下单日期相同或者在那之后)。

如果顾客期望的配送日期和下单日期相同,则该订单称为 「即时订单」,否则称为「计划订单」。

首次订单」是顾客最早创建的订单。我们保证一个顾客只会有一个「首次订单」。

编写解决方案以获取即时订单在所有用户的首次订单中的比例。保留两位小数。

结果示例如下所示:

示例 1:

输入:
Delivery 表:
+-------------+-------------+------------+-----------------------------+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
+-------------+-------------+------------+-----------------------------+
| 1           | 1           | 2019-08-01 | 2019-08-02                  |
| 2           | 2           | 2019-08-02 | 2019-08-02                  |
| 3           | 1           | 2019-08-11 | 2019-08-12                  |
| 4           | 3           | 2019-08-24 | 2019-08-24                  |
| 5           | 3           | 2019-08-21 | 2019-08-22                  |
| 6           | 2           | 2019-08-11 | 2019-08-13                  |
| 7           | 4           | 2019-08-09 | 2019-08-09                  |
+-------------+-------------+------------+-----------------------------+
输出:
+----------------------+
| immediate_percentage |
+----------------------+
| 50.00                |
+----------------------+
解释:
1 号顾客的 1 号订单是首次订单,并且是计划订单。
2 号顾客的 2 号订单是首次订单,并且是即时订单。
3 号顾客的 5 号订单是首次订单,并且是计划订单。
4 号顾客的 7 号订单是首次订单,并且是即时订单。
因此,一半顾客的首次订单是即时的。

4.2 思路:

看注释。

4.3 题解:

sql">select round(count(*) / (select count(distinct customer_id) from Delivery) * 100, 2) immediate_percentage 
from (select *from Deliverywhere order_date = customer_pref_delivery_date
) t
where order_date = (select min(order_date)from Delivery d2where t.customer_id = d2.customer_id
)--先找到顾客的总人数
-- select count(distinct customer_id) 
-- from Delivery--再找可能满足条件的记录(里面可能有即使订单,也有计划订单)
-- select *
-- from Delivery
-- where order_date = customer_pref_delivery_date--从上述的表中查找是即时订单的个数,计划订单用where条件过滤。
-- where order_date = (
--     select min(order_date)
--     from Delivery d2
--     where t.customer_id = d2.customer_id
-- )


http://www.ppmy.cn/devtools/120546.html

相关文章

利用 Page Visibility API 优化网页性能与用户体验

在现代 Web 开发中&#xff0c;用户可能会频繁切换标签页&#xff0c;或让网页处于后台运行。为了避免不必要的资源浪费并提升用户体验&#xff0c;合理利用 Page Visibility API 可以在页面不可见时暂停或减少资源的消耗&#xff0c;并在页面重新可见时恢复正常操作。 在这篇…

将给定的表达式树(二叉树)转换为等价的中缀表达式(通过括号反映操作符的计算次序)并输出

请设计一个算法&#xff0c;将给定的表达式树&#xff08;二叉树&#xff09;转换为等价的中缀表达式&#xff08;通过括号反映操作符的计算次序&#xff09;并输出。例如&#xff0c;当下列两棵表达式树作为算法输入时&#xff1a; 输出的中缀表达式分别为 (ab)∗(c∗(−d)) 和…

C# winform s7.net 类读取 报错:数组不是一维数组。

System.ArgumentException HResult0x80070057 Message数组不是一维数组。 Sourcemscorlib StackTrace: at System.Array.SetValue(Object value, Int32 index) at S7.Net.Types.Class.FromBytes(Object sourceClass, Byte[] bytes, Double numBytes, Boolean isI…

使用VBA快速将文本转换为Word表格

Word提供了一个强调的文本转表格的功能&#xff0c;结合VBA可以实现文本快速转换表格。 示例文档如下所示。 现在需要将上述文档内容转换为如下格式的表格&#xff0c;表格内容的起始标志为。 示例代码如下。 Sub SearchTab()Application.DefaultTableSeparator "*&quo…

介绍篇| 爬虫工具介绍

什么是网络爬虫 网络爬虫工具本质上是自动化从网站提取数据的软硬件或服务。它简化了网络爬虫,使信息收集变得更加容易。如今是数据和智能化时代, 如何快速、自动化获取数据, 成了个人或者企业进入智能化时代的第一步. 选择最佳网络爬虫工具时的关键因素 在选择最佳网络爬虫…

前端安装 lerna

当你在终端中遇到 lerna: command not found 错误时&#xff0c;意味着你的系统没有找到 lerna 命令。这通常是因为 lerna 没有被正确安装或者其可执行文件的路径没有被添加到系统的 PATH 环境变量中。以下是解决这个问题的步骤&#xff1a; 1. 确认 Lerna 是否已安装 首先&a…

VUE前后端分离毕业设计题目项目有哪些,VUE程序开发常见毕业论文设计推荐

目录 0 为什么选择Vue.js 1 Vue.js 的主要特点 2 前后端分离毕业设计项目推荐 3 后端推荐 4 总结 0 为什么选择Vue.js 使用Vue.js开发计算机毕业设计是一个很好的选择&#xff0c;因为它不仅具有现代前端框架的所有优点&#xff0c;还能让你专注于构建高性能、高可用性的W…

Python 解析 html

一、场景分析 假设有如下 html 文档&#xff1a; 写一段 python 脚本&#xff0c;解析出里面的数据&#xff0c;包括经度维度。 <div classstorelist><ul><li lng"100.111111" lat"10.111111"><h4>联盟店1</h4><p>…