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

结果:


总结:

能运行就行。


相关推荐
pjx98711 分钟前
应用的“体检”与“换装”:精通Spring Boot配置管理与Actuator监控
数据库·spring boot·oracle
松树戈30 分钟前
PostgreSQL 分区表——范围分区SQL实践
数据库·sql·postgresql
bing_15840 分钟前
Redis 的单线程模型对微服务意味着什么?需要注意哪些潜在瓶颈?
数据库·redis·微服务
wangzhongyudie43 分钟前
SQL实战:01之行转列实现
数据库·sql
斗鹰一余洛晟1 小时前
工作中sql总结
数据库·sql
阿七想学习1 小时前
MySQL《事务》
数据库·mysql
Yang_yangyang1 小时前
mysql定时备份、打包、传输
数据库·mysql·自动化
到底怎么取名字不会重复1 小时前
Day10——LeetCode15&560
c++·算法·leetcode·哈希算法·散列表
大大大水蜜桃1 小时前
DNS主从同步及解析
数据库
DKPT1 小时前
正则表达式
java·数据库·笔记·学习·正则表达式