1084. Sales Analysis III 难度:简单

1、题目描述

Write an SQL query that reports the products that were only sold in spring 2019. That is, between 2019-01-01 and 2019-03-31 inclusive.

The query result format is in the following example:

Product table:

product_idproduct_nameunit_price
1S81000
2G4800
3iPhone1400

Sales table:

seller_idproduct_idbuyer_idsale_datequantityprice
1112019-01-2122000
1222019-02-171800
2232019-06-021800
3342019-05-1322800

Result table:

product_idproduct_name
1S8

The product with id 1 was only sold in spring 2019 while the other two were sold after.

来源:力扣(LeetCode)

2、解题思路

更上一题差不多的思路
1# 首先,增加子表,查询所有不在'2019-01-01' and '2019-03-31'之间销售的product_id

select p.product_id
from Product p right join Sales s
on s.product_id=p.product_id
where sale_date not between '2019-01-01' and '2019-03-31'

2# 然后,两表联查,条件是日期在'2019-01-01' and '2019-03-31'之间,并且不在上述范围

3、提交记录

select distinct p.product_id,product_name
from Product p right join Sales s
on s.product_id=p.product_id
where sale_date between '2019-01-01' and '2019-03-31'
and p.product_id not in (
select p.product_id
from Product p right join Sales s
on s.product_id=p.product_id
where sale_date not between '2019-01-01' and '2019-03-31')
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值