【力扣刷题日记】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)

结果:


总结:

能运行就行。


相关推荐
optimistic_chen11 分钟前
【Redis 系列】常用数据结构---ZSET类型
数据结构·数据库·redis·xshell·zset·redis命令
cike_y13 分钟前
Spring整合Mybatis:dao层
java·开发语言·数据库·spring·mybatis
小蒜学长15 分钟前
足球联赛管理系统(代码+数据库+LW)
java·数据库·spring boot·后端
松涛和鸣16 分钟前
45、无依赖信息查询系统(C语言+SQLite3+HTML)
c语言·开发语言·数据库·单片机·sqlite·html
智航GIS19 分钟前
9.2 多进程入门
数据库·python
DemonAvenger20 分钟前
Redis与微服务:分布式系统中的缓存设计模式
数据库·redis·性能优化
柒.梧.23 分钟前
Spring JDBC实战指南:从基础操作到事务管理全解析
数据库·oracle
rchmin26 分钟前
PostgreSQL与MySQL选型对比
数据库·mysql·postgresql
Data_agent28 分钟前
微店商品列表API接口指南
大数据·数据库·python
Vic1010129 分钟前
PostgreSQL 中序列(bigserial 和手动序列)的使用与注意事项
java·大数据·数据库·postgresql