跳到主要内容

1084.销售分析 III

序号题目难度
1084题目链接简单

题目大意​

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

以 任意顺序 返回结果表。

表: Product

Column NameType
product_idint
product_namevarchar
unit_priceint

product_id 是该表的主键(具有唯一值的列)。 该表的每一行显示每个产品的名称和价格。

表:Sales

Column NameType
seller_idint
product_idint
buyer_idint
sale_datedate
quantityint
priceint

这个表可能有重复的行。 product_id 是 Product 表的外键(reference 列)。 该表的每一行包含关于一个销售的一些信息。

示例​

输入​

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

输出​

product_idproduct_name
1S8

解释​

id 为 1 的产品仅在 2019 年春季销售。 id 为 2 的产品在 2019 年春季销售,但也在 2019 年春季之后销售。 id 为 3 的产品在 2019 年春季之后销售。 我们只返回 id 为 1 的产品,因为它是 2019 年春季才销售的产品。

参考答案​

with t0 as (
select product_id from Sales
where !(sale_date between '2019-01-01' and '2019-03-31')
group by 1
)
select
t1.product_id
,t2.product_name
from Sales t1
left join Product t2
on t1.product_id = t2.product_id
left join t0
on t0.product_id = t1.product_id
where t1.sale_date between '2019-01-01' and '2019-03-31'
and t0.product_id is null
group by 1,2
;