Leetcode1384.按年度列出销售总额(困难)

这道题确实值得困难的难度
也很有意思
值得慢慢来做

问题
Table: Product

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| product_id    | int     |
| product_name  | varchar |
+---------------+---------+

product_id is the primary key for this table.
product_name is the name of the product.

Table: Sales

+---------------------+---------+
| Column Name         | Type    |
+---------------------+---------+
| product_id          | int     |
| period_start        | varchar |
| period_end          | date    |
| average_daily_sales | int     |
+---------------------+---------+

product_id is the primary key for this table.
period_start and period_end indicates the start and end date for sales period, both dates are inclusive.
The average_daily_sales column holds the average daily sales amount of the items for the period.

Write an SQL query to report the Total sales amount of each item for each year, with corresponding product name, product_id, product_name and report_year.

Dates of the sales years are between 2018 to 2020. Return the result table ordered by product_id and report_year.

The query result format is in the following example:

Product table:

+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 1          | LC Phone     |
| 2          | LC T-Shirt   |
| 3          | LC Keychain  |
+------------+--------------+

Sales table:

+------------+--------------+-------------+---------------------+
| 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 was sold for the period of 2019-01-25 to 2019-02-28, and there are 35 days for this period. Total amount 35*100 = 3500.
LC T-shirt was sold for the period of 2018-12-01 to 2020-01-01, and there are 31, 365, 1 days for years 2018, 2019 and 2020 respectively.
LC Keychain was sold for the period of 2019-12-01 to 2020-01-31, and there are 31, 31 days for years 2019 and 2020 respectively.

生成数据

Create table If Not Exists Product (product_id int, product_name varchar(30))
Create table If Not Exists Sales (product_id varchar(30), period_start date, period_end date, average_daily_sales int)
Truncate table Product
insert into Product (product_id, product_name) values ('1', 'LC Phone ')
insert into Product (product_id, product_name) values ('2', 'LC T-Shirt')
insert into Product (product_id, product_name) values ('3', 'LC Keychain')
Truncate table Sales
insert into Sales (product_id, period_start, period_end, average_daily_sales) values ('1', '2019-01-25', '2019-02-28', '100')
insert into Sales (product_id, period_start, period_end, average_daily_sales) values ('2', '2018-12-01', '2020-01-01', '10')
insert into Sales (product_id, period_start, period_end, average_daily_sales) values ('3', '2019-12-01', '2020-01-31', '1')

解答
不太会
看着别人的答案摸索一下

先构造一个集合 每一年的年份 和对应起始日期和结束日期

SELECT 
  '2018' AS report_year,
  DATE('2018-01-01') AS start_date,
  DATE('2018-12-31') AS end_date 
FROM
  DUAL 
UNION
ALL 
SELECT 
  '2019' AS YEAR,
  DATE('2019-01-01') AS start_date,
  DATE('2019-12-31') AS end_date 
FROM
  DUAL 
UNION
ALL 
SELECT 
  '2020' AS YEAR,
  DATE('2020-01-01') AS start_date,
  DATE('2020-12-31') AS end_date 
FROM
  DUAL 

from dual 是一个虚拟表 纯粹是为了满足select … from…这一习惯问题,mysql会忽略对该表的引用。

有三种情况


  1. 销售区间包含自然年的前半段
    这种情况下是period_end与start_date的差值加一就是所需天数
  2. 销售区间包含自然年的后半段
    这种情况下 end_date与period_start的差值加一就是需要的天数
  3. 销售区间包含自然年的全部
    这时period_end与period_start的差值+1即可

以上表与sales表按以上三种情况进行连接

SELECT *
FROM Sales1 AS S, (SELECT '2018'             AS report_year,
                DATE('2018-01-01') AS start_date,
                DATE('2018-12-31') AS end_date
         FROM DUAL
         UNION ALL
         SELECT '2019'             AS YEAR,
                DATE('2019-01-01') AS start_date,
                DATE('2019-12-31') AS end_date
         FROM DUAL
         UNION ALL
         SELECT '2020'             AS YEAR,
                DATE('2020-01-01') AS start_date,
                DATE('2020-12-31') AS end_date
         FROM DUAL) AS t
WHERE   # 销售区间包含自然年的后半段
    S.`period_start` BETWEEN t.start_date AND t.end_date 
    # 销售区间包含自然年的前半段
    OR  S.`period_end` BETWEEN t.start_date AND t.end_date 
    # 整个自然年都在销售区间之内
    OR  (S.`period_start`>= t.start_date AND S.`period_end` <= t.end_date);

我开始的想法是根据三种分类 分别求出三种的天数差
但是是行不通的
首先 忽略了这种情况 比如20190401- 2019-0501 满足销售区间包含自然年的后半段 也满足销售区间包含自然年的前半段
但是其计算方式 只要period_end与period_start做时间差即可

其实发现上边是有"规律"的
1.都是end与start 求datediff
2.end取得是两个end中最小的 start则取得是两个start中的最大的
时间差记得加1

SELECT S.product_id, t.report_year,
(DATEDIFF(IF(S.`period_end`< t.end_date, S.`period_end`,t.end_date),
IF(S.`period_start` < t.start_date, t.start_date, S.`period_start`))+ 1) *S.`average_daily_sales` AS total_amount
FROM Sales1 AS S, (SELECT '2018'             AS report_year,
                DATE('2018-01-01') AS start_date,
                DATE('2018-12-31') AS end_date
         FROM DUAL
         UNION ALL
         SELECT '2019'             AS YEAR,
                DATE('2019-01-01') AS start_date,
                DATE('2019-12-31') AS end_date
         FROM DUAL
         UNION ALL
         SELECT '2020'             AS YEAR,
                DATE('2020-01-01') AS start_date,
                DATE('2020-12-31') AS end_date
         FROM DUAL) AS t
WHERE   # 销售区间包含自然年的后半段
    S.`period_start` BETWEEN t.start_date AND t.end_date 
    # 销售区间包含自然年的前半段
    OR  S.`period_end` BETWEEN t.start_date AND t.end_date 
    # 整个自然年都在销售区间之内
    OR  (t.start_date > S.`period_start` AND t.end_date < S.`period_end`);

最后当然是简单的两表连接啦

SELECT P.product_id, P.`product_name`, tmp.report_year, tmp.total_amount
FROM (
SELECT S.product_id, t.report_year,
(DATEDIFF(IF(S.`period_end`< t.end_date, S.`period_end`,t.end_date),
IF(S.`period_start` < t.start_date, t.start_date, S.`period_start`))+ 1) *S.`average_daily_sales` AS total_amount
FROM Sales1 AS S, (SELECT '2018'             AS report_year,
                DATE('2018-01-01') AS start_date,
                DATE('2018-12-31') AS end_date
         FROM DUAL
         UNION ALL
         SELECT '2019'             AS YEAR,
                DATE('2019-01-01') AS start_date,
                DATE('2019-12-31') AS end_date
         FROM DUAL
         UNION ALL
         SELECT '2020'             AS YEAR,
                DATE('2020-01-01') AS start_date,
                DATE('2020-12-31') AS end_date
         FROM DUAL) AS t
WHERE   # 销售区间包含自然年的后半段
    S.`period_start` BETWEEN t.start_date AND t.end_date 
    # 销售区间包含自然年的前半段
    OR  S.`period_end` BETWEEN t.start_date AND t.end_date 
    # 整个自然年都在销售区间之内
    OR  (t.start_date > S.`period_start` AND t.end_date < S.`period_end`)
) AS tmp
JOIN Product3 AS P
ON P.product_id = tmp.product_id
ORDER BY P.product_id, P.`product_name`;
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,525评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,203评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,862评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,728评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,743评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,590评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,330评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,244评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,693评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,885评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,001评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,723评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,343评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,919评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,042评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,191评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,955评论 2 355