【力扣刷题日记】1082.销售分析I

前言

练习sql语句,所有题目来自于力扣(https://leetcode.cn/problemset/database/)的免费数据库练习题。

今日题目:

1082.销售分析I

表:Person

列名 类型
product_id int
product_name varchar
unit_price int

product_id 是这个表的主键(具有唯一值的列)。

该表的每一行显示每个产品的名称和价格。

表:Sales

列名 类型
seller_id int
product_id int
buyer_id int
sale_date date
quantity int
price int

这个表它可以有重复的行。 product_id 是 Product 表的外键(reference 列)。

该表的每一行包含关于一个销售的一些信息。


我那不值一提的想法:

  • 首先梳理表内容,题干一共给了一张产品表,记录了产品id,产品名,单价,一张销售表,记录了销售id,产品id,购买者id,销售日期,销售数量,销售价格。
  • 其次分析需求,需要找到总销售额最高的销售者,如果并列,一同显示。
  • 我们首先需要计算,每个销售id的销售总金额,并建立临时表
  • 其次查询这张表中的seller_id,条件就是这张表的all_price = 这张表的max(all_price) 得到最终结果
sql 复制代码
with all_price as (
select seller_id,sum(price) as all_price
from Sales
group by seller_id
)

select seller_id
from all_price
where all_price = 
(select max(all_price)
from all_price)

结果:


总结:

能运行就行。


相关推荐
【非典型Coder】10 分钟前
Statement和PreparedStatement区别
数据库
Emilia486.21 分钟前
【Leetcode&nowcode&数据结构】顺序表的应用
数据结构·算法·leetcode
m0_736927041 小时前
想抓PostgreSQL里的慢SQL?pg_stat_statements基础黑匣子和pg_stat_monitor时间窗,谁能帮你更准揪出性能小偷?
java·数据库·sql·postgresql
lang201509281 小时前
MySQL 8.0.29 及以上版本中 SSL/TLS 会话复用(Session Reuse)
数据库·mysql
望获linux2 小时前
【实时Linux实战系列】使用 u-trace 或 a-trace 进行用户态应用剖析
java·linux·前端·网络·数据库·elasticsearch·操作系统
Dream it possible!2 小时前
LeetCode 面试经典 150_栈_简化路径(53_71_C++_中等)(栈+stringstream)
c++·leetcode·面试·
程序员阿鹏2 小时前
49.字母异位词分组
java·开发语言·leetcode
清和与九2 小时前
binLog、redoLog和undoLog的区别
数据库·oracle
Espresso Macchiato2 小时前
Leetcode 3715. Sum of Perfect Square Ancestors
算法·leetcode·职场和发展·leetcode hard·树的遍历·leetcode 3715·leetcode周赛471
望获linux2 小时前
【实时Linux实战系列】FPGA 与实时 Linux 的协同设计
大数据·linux·服务器·网络·数据库·fpga开发·操作系统