2013-10-21 27 views
4

我試圖查詢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年1月1日,集團按類別。 試圖創建輸出由分類銷售的查詢時,該問題是,「子類別2」被改變那裏的Parent_Category/PARENT_ID並且結果必須是2行爲‘產品1’

結果

+-----------+------------+----------------+-----------+------------+------------+------+ 
| 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 | 
+-----------+------------+----------------+-----------+------------+------------+------+ 

回答

0

這是一個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修復,可能會出現問題。另外,我假設產品只在子類別下,而不在類別下。