按日期范围历史生成完整的类别结构

Posted

技术标签:

【中文标题】按日期范围历史生成完整的类别结构【英文标题】:Generate full category structure by date range history 【发布时间】:2013-10-29 16:04:39 【问题描述】:

我正在尝试查询 mysql。我有 2 个表,数据如下所示:

category_history_structure

+----------------+-----------------+----------+----+-----------+------------+------------+
| category       | parent_category | type     | id | parent_id | from_date  | to_date    |
+----------------+-----------------+----------+----+-----------+------------+------------+
| Top level      |                 | category | 1  | 0         | 01.01.2013 | 01.01.2015 |
+----------------+-----------------+----------+----+-----------+------------+------------+
| Category 1     | Top level       | category | 2  | 1         | 01.01.2013 | 01.01.2015 |
+----------------+-----------------+----------+----+-----------+------------+------------+
| Category 2     | Top level       | category | 3  | 1         | 01.01.2013 | 01.01.2015 |
+----------------+-----------------+----------+----+-----------+------------+------------+
| Sub category 1 | Category 1      | category | 4  | 2         | 01.01.2013 | 01.01.2015 |
+----------------+-----------------+----------+----+-----------+------------+------------+
| Sub category 2 | Category 1      | category | 5  | 2         | 01.01.2013 | 01.03.2013 |
+----------------+-----------------+----------+----+-----------+------------+------------+
| Sub category 2 | Category 2      | category | 5  | 3         | 02.03.2013 | 01.01.2015 |
+----------------+-----------------+----------+----+-----------+------------+------------+
| Product 1      | Sub category 2  | product  | 6  | 5         | 01.01.2013 | 01.01.2015 |
+----------------+-----------------+----------+----+-----------+------------+------------+

product_sells

+----+-----------+------+------------+
| id | product   | sell | date       |
+----+-----------+------+------------+
| 6  | Product 1 | 2    | 01.02.2013 |
+----+-----------+------+------------+
| 6  | Product 1 | 1    | 01.05.2013 |
+----+-----------+------+------------+
| 6  | Product 1 | 3    | 01.06.2013 |
+----+-----------+------+------------+

我需要从日期范围 2013-01-01 - 2015-01-01 开始销售,按类别分组。 尝试创建按类别输出销售的查询,问题是“子类别 2”已更改为 Parent_Category/parent_id,结果必须在“产品 1”的 2 行中

结果

+-----------+------------+----------------+-----------+------------+------------+------+
| Top level | Category   | Sub category   | Product   | from_date  | to_date    | sell |
+-----------+------------+----------------+-----------+------------+------------+------+
| Top level | Category 1 | Sub category 2 | Product 1 | 01.01.2013 | 01.03.2013 | 2    |
+-----------+------------+----------------+-----------+------------+------------+------+
| Top level | Category 2 | Sub category 2 | Product 1 | 02.03.2013 | 01.01.2015 | 4    |
+-----------+------------+----------------+-----------+------------+------------+------+

【问题讨论】:

【参考方案1】:

这是一个 sqlfiddle: http://sqlfiddle.com/#!2/cdb68/3。 我把日期放在 mysql 格式中。我假设“01.06.2013”​​表示“2013 年 6 月 1 日”或“2013-06-01”。

SELECT 
'Top level',
cat.parent_category as category, 
subcat.parent_category as subcategory, 
ps.product,
cat.from_date, cat.to_date, 
SUM(ps.sell)


FROM product_sells AS ps
LEFT JOIN category_history_structure as subcat
ON (
  ps.id = subcat.id
  AND ps.date BETWEEN subcat.from_date AND subcat.to_date
  AND subcat.type = 'product'
  )
LEFT JOIN category_history_structure as cat
ON (
  subcat.parent_id = cat.id
  AND ps.date BETWEEN cat.from_date AND cat.to_date
  AND cat.type = 'category'
  )

GROUP BY category;

如果***类别也发生了变化,可能会出现问题,但这可以通过另一个 LEFT JOIN 来解决。另外,我假设产品只属于子类别,而不是直接属于类别。

【讨论】:

以上是关于按日期范围历史生成完整的类别结构的主要内容,如果未能解决你的问题,请参考以下文章

DATEDIFF 按创建日期划分范围,利用外部日期按月计算年龄历史

Python数据分析pandas日期范围date_range

Pandas 分别对每个类别的日期范围求和

按类别选择标记和适合可见标记的范围

SQL 计算项目状态历史记录在日期范围内的项目项

Bigquery - 为每个 id 添加完整的日期范围