要求:编写一段 SQL 查询每个产品每年的总销售额,并包含 product_id, product_name 以及 report_year 等信息。
已知:销售年份的日期介于 2018 年到 2020 年之间。你返回的结果需要按 product_id 和 report_year 排序。
Product 表的结构:
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| product_id | int |
| product_name | varchar |
+---------------+---------+
product_id 是这张表的主键。
product_name 是产品的名称。
Sales 表的结构:
+---------------------+---------+
| Column Name | Type |
+---------------------+---------+
| product_id | int |
| period_start | date |
| period_end | date |
| average_daily_sales | int |
+---------------------+---------+
product_id 是这张表的主键。
period_start 和 period_end 是该产品销售期的起始日期和结束日期,且这两个日期包含在销售期内。
average_daily_sales 列存储销售期内该产品的日平均销售额。
Product 表:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 1 | LC Phone |
| 2 | LC T-Shirt |
| 3 | LC Keychain |
+------------+--------------+
Sales 表:
+------------+--------------+-------------+---------------------+
| product_id | period_start | period_end | average_daily_sales |
+------------+--------------+-------------+---------------------+
| 1 | 2019-01-25 | 2019-02-28 | 100 |
| 2 | 2018-12-01 | 2020-01-01 | 10 |
| 3 | 2019-12-01 | 2020-01-31 | 1 |
+------------+--------------+-------------+---------------------+
Result Table:
+------------+--------------+-------------+--------------+
| product_id | product_name | report_year | total_amount |
+------------+--------------+-------------+--------------+
| 1 | LC Phone | 2019 | 3500 |
| 2 | LC T-Shirt | 2018 | 310 |
| 2 | LC T-Shirt | 2019 | 3650 |
| 2 | LC T-Shirt | 2020 | 10 |
| 3 | LC Keychain | 2019 | 31 |
| 3 | LC Keychain | 2020 | 31 |
+------------+--------------+-------------+--------------+
LC Phone 在 2019-01-25 至 2019-02-28 期间销售,该产品销售时间总计35天。销售总额 35*100 = 3500。
LC T-shirt 在 2018-12-01 至 2020-01-01 期间销售,该产品在2018年、2019年、2020年的销售时间分别是31天、365天、1天,2018年、2019年、2020年的销售总额分别是31*10=310、365*10=3650、1*10=10。
LC Keychain 在 2019-12-01 至 2020-01-31 期间销售,该产品在2019年、2020年的销售时间分别是:31天、31天,2019年、2020年的销售总额分别是31*1=31、31*1=31。
SQL语句:
select a.product_id,b.product_name,substr(p1,1,4) as report_year,
(datediff(p2,p1)+1)*av1 as total_amount
from
(
(select product_id,period_start as p1,period_end as p2,average_daily_sales as av1
from sales
where substr(period_end,1,4)-substr(period_start,1,4)=0
)
union all
(select product_id,period_start as p1,concat(substr(period_start,1,4),'-12-31')as p2,average_daily_sales as av1
from sales
where substr(period_end,1,4)-substr(period_start,1,4)=1
)union all
(select product_id,concat(substr(period_end,1,4),'-01-01')as p1,period_end as p2,average_daily_sales as av1
from sales
where substr(period_end,1,4)-substr(period_start,1,4)=1
)
union all
(select product_id,period_start as p1,concat(substr(period_start,1,4),'-12-31')as p2,average_daily_sales as av1
from sales
where substr(period_end,1,4)-substr(period_start,1,4)=2
)union all
(select product_id,concat(substr(period_start,1,4)+1,'-01-01')as p1,concat(substr(period_start,1,4)+1,'-12-31')as p2,average_daily_sales as av1
from sales
where substr(period_end,1,4)-substr(period_start,1,4)=2
)union all
(select product_id,concat(substr(period_end,1,4),'-01-01')as p1,period_end as p2,average_daily_sales as av1
from sales
where substr(period_end,1,4)-substr(period_start,1,4)=2))a
join product b
on a.product_id=b.product_id
order by a.product_id asc,report_year asc