当前位置: 首页 > news >正文

SQL-leetcode—1084. 销售分析 III

1084. 销售分析 III

表: Product

±-------------±--------+
| Column Name | Type |
±-------------±--------+
| product_id | int |
| product_name | varchar |
| unit_price | int |
±-------------±--------+
product_id 是该表的主键(具有唯一值的列)。
该表的每一行显示每个产品的名称和价格。
表:Sales

±------------±--------+
| Column Name | Type |
±------------±--------+
| seller_id | int |
| product_id | int |
| buyer_id | int |
| sale_date | date |
| quantity | int |
| price | int |
±----- ------±--------+
这个表可能有重复的行。
product_id 是 Product 表的外键(reference 列)。
该表的每一行包含关于一个销售的一些信息。

编写解决方案,报告 2019年春季 才售出的产品。即 仅 在 2019-01-01 (含)至 2019-03-31 (含)之间出售的商品。

以 任意顺序 返回结果表。

结果格式如下所示。

示例 1:

输入:
Product table:
±-----------±-------------±-----------+
| product_id | product_name | unit_price |
±-----------±-------------±-----------+
| 1 | S8 | 1000 |
| 2 | G4 | 800 |
| 3 | iPhone | 1400 |
±-----------±-------------±-----------+
Sales table:
±----------±-----------±---------±-----------±---------±------+
| seller_id | product_id | buyer_id | sale_date | quantity | price |
±----------±-----------±---------±-----------±---------±------+
| 1 | 1 | 1 | 2019-01-21 | 2 | 2000 |
| 1 | 2 | 2 | 2019-02-17 | 1 | 800 |
| 2 | 2 | 3 | 2019-06-02 | 1 | 800 |
| 3 | 3 | 4 | 2019-05-13 | 2 | 2800 |
±----------±-----------±---------±-----------±---------±------+
输出:
±------------±-------------+
| product_id | product_name |
±------------±-------------+
| 1 | S8 |
±------------±-------------+
解释:
id 为 1 的产品仅在 2019 年春季销售。
id 为 2 的产品在 2019 年春季销售,但也在 2019 年春季之后销售。
id 为 3 的产品在 2019 年春季之后销售。
我们只返回 id 为 1 的产品,因为它是 2019 年春季才销售的产品。

题解

编写解决方案,报告 2019年春季 才售出的产品。即 仅 在 2019-01-01 (含)至 2019-03-31 (含)之间出售的商品。

仅 在 2019-01-01 (含)至 2019-03-31 (含)之间出售的商品
意味着只在在 2019-01-01 (含)至 2019-03-31 (含)之间,且不再这个范围以外出售

方法一 where + 子查询

select product_id,product_name from Product where product_id in (selectproduct_idfrom Sales -- 判断 仅 在 2019-01-01 (含)至 2019-03-31 (含)之间出售where sale_date between '2019-01-01' and '2019-03-31' and product_id not in (select product_id from Sales where sale_date<'2019-01-01' or sale_date>'2019-03-31')
)

方法二 求产品的最值,如果均在范围内即可

select product_id,product_name from Product where product_id in (selectproduct_idfrom Sales -- 判断 仅 在 2019-01-01 (含)至 2019-03-31 (含)之间出售group by product_idhaving (min(sale_date) between '2019-01-01' and '2019-03-31') and (max(sale_date) between '2019-01-01' and '2019-03-31')
)
http://www.lryc.cn/news/524753.html

相关文章:

  • Linux C\C++编程-文件位置指针与读写文件数据块
  • Flask简介与安装以及实现一个糕点店的简单流程
  • 【自动化测试】—— Appium使用保姆教程
  • 西门子【Library of General Functions (LGF) for SIMATIC S7-1200 / S7-1500】
  • IntelliJ IDEA 2023.3 中配置 Spring Boot 项目的热加载
  • Python----Python高级(正则表达式:语法规则,re库)
  • 通过Ukey或者OTP动态口令实现windows安全登录
  • Node.js接收文件分片数据并进行合并处理
  • Lsky-Pro在线图片搭建教程(Docker部署方式)
  • “深入浅出”系列之算法篇:(2)openCV、openMV、openGL
  • AI 新动态:技术突破与应用拓展
  • 从CRUD到高级功能:EF Core在.NET Core中全面应用(三)
  • 【记录】Jenkins版本及JDK关系介绍的官网地址
  • vue3-json-viewer和vue-json-pretty插件使用,vue3 json数据美化展示
  • python转转商超书籍信息爬虫
  • Spring Boot 中的 InitializingBean:Bean 初始化背后的故事
  • 微信小程序:实现单选,多选,通过变量控制单选/多选
  • MOS怎样选型,步骤详解
  • CMake技术细节:解决未定义,提供参数
  • 1688 满足跨境业务需求而提供的一组 API 接口
  • 物联网网关Web服务器--CGI开发实例BMI计算
  • 计算机网络 (51)鉴别
  • 【Docker】搭建一个功能强大的自托管虚拟浏览器 - n.eko
  • 论文笔记(六十二)Diffusion Reward Learning Rewards via Conditional Video Diffusion
  • 探索 Stable-Diffusion-Webui-Forge:更快的AI图像生成体验
  • Redis使用基础
  • PyCharm+RobotFramework框架实现UDS自动化测试- (四)项目实战0x10
  • 【TCP】rfc文档
  • 【SpringCloud】黑马微服务学习笔记
  • 梯度提升决策树树(GBDT)公式推导