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

时间:2013-10-21 08:46:54

标签: mysql join date-range

我正在尝试查询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 个答案:

答案 0 :(得分:0)

这是一个方形的: 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修复。此外,我假设产品只属于子类别,而不是直接属于类别。