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

结果:


总结:

能运行就行。


相关推荐
悄悄敲敲敲6 小时前
MySQL表的约束
数据库·mysql
鼠爷ねずみ6 小时前
SpringCloud前后端整体开发流程-以及技术总结文章实时更新中
java·数据库·后端·spring·spring cloud
九皇叔叔7 小时前
MySQL 数据库 Read View 详解
数据库·mysql·mvcc·read view
前端小白在前进8 小时前
力扣刷题:在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·leetcode
Elastic 中国社区官方博客8 小时前
Elasticsearch:圣诞晚餐 BBQ - 图像识别
大数据·数据库·elasticsearch·搜索引擎·ai·全文检索
cui_win8 小时前
Prometheus实战教程 - Redis 监控
数据库·redis·prometheus
JIngJaneIL9 小时前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
TG:@yunlaoda360 云老大9 小时前
华为云国际站代理商备份策略设置过程中遇到问题如何解决?
服务器·数据库·华为云
SelectDB9 小时前
Doris Catalog 已上线!性能提升 200x,全面优于 JDBC Catalog,跨集群查询迈入高性能分析时代
数据库·数据分析·apache
iAkuya9 小时前
(leetcode)力扣100 23反转链表(迭代||递归)
算法·leetcode·链表