我有一个数据仓库,其中SELECT(和SUM)查询具有以下输出。
+------+-----------+-------------+------------+
| YEAR | ITEM TYPE | TOTAL_ITEMS | TOTAL_COST |
+------+-----------+-------------+------------+
| 2009 | TYPE-1 | 19 | 330 |
| 2009 | TYPE-2 | 1 | 10 |
| 2009 | TYPE-3 | 11 | 190 |
| 2010 | TYPE-1 | 11 | 220 |
| 2010 | TYPE-2 | 7 | 230 |
| 2010 | TYPE-3 | 3 | 360 |
+------+-----------+-------------+------------+
我的问题是如何创建一个新列,以百分比形式计算2009年和2010年(以2009年为基数)之间的总成本差异。
所以输出将是这样的:
+------+-----------+-------------+------------+----------+----------+
| YEAR | ITEM TYPE | TOTAL_ITEMS | TOTAL_COST | ItemDiff | CostDiff |
+------+-----------+-------------+------------+----------+----------+
| 2009 | TYPE-1 | 19 | 330 | 0% | 0 |
| 2009 | TYPE-2 | 1 | 10 | 0% | 0 |
| 2009 | TYPE-3 | 11 | 190 | 0% | 0 |
| 2010 | TYPE-1 | 11 | 220 | -42.11% | -33.33% |
| 2010 | TYPE-2 | 7 | 230 | 1000% | 2200% |
| 2010 | TYPE-3 | 3 | 360 | -72.73% | 80.47% |
+------+-----------+-------------+------------+----------+----------+
“项目类型”是一个类别,由多个价格不同的商品组成。我需要计算每个类别而不是每个项目的更改。
到目前为止,我的查询是
SELECT
date_dim.year,
item_dim.item_type,
SUM(fact.total_item)TotalItems,
SUM(fact.total_cost) AS TotalCost
FROM fact
INNER JOIN date_dim
ON fact.date_key = date_dim.date_key
INNER JOIN item_dim
ON fact.item_key = item_dim.item_key
WHERE date_dim.year BETWEEN 2009 AND 2011
GROUP BY date_dim.year,
item_dim.item_type
请查看下面的小提琴,其中已经构建了模式和查询。
这是简化的ERD ...
预先感谢您的帮助...
答案 0 :(得分:1)
以下是如何实现此目的的查询:
使用MySQL公共表表达式(此操作无法在sqlfiddle上运行)
WITH summary_table AS
(SELECT
substr(date_dim.year,1,4) year,
item_dim.item_type,
SUM(fact.total_item) TotalItems,
SUM(fact.total_cost) AS TotalCost
FROM fact
INNER JOIN date_dim
ON fact.date_key = date_dim.date_key
INNER JOIN item_dim
ON fact.item_key = item_dim.item_key
WHERE date_dim.year BETWEEN 2009 AND 2011
GROUP BY date_dim.year,
item_dim.item_type)
SELECT
A.*,
CASE WHEN (A.TotalItems IS NULL OR B.TotalItems IS NULL OR B.TotalItems=0) THEN 0 ELSE
(A.TotalItems - B.TotalItems)*100/B.TotalItems END AS ItemDiff,
CASE WHEN (A.TotalCost IS NULL OR B.TotalCost IS NULL OR B.TotalCost=0) THEN 0 ELSE
(A.TotalCost - B.TotalCost)*100/B.TotalCost END AS CostDiff
FROM summary_table A LEFT JOIN summary_table B
ON A.YEAR=(B.YEAR+1) AND A.ITEM_TYPE=B.ITEM_TYPE;
没有CTE (请参见demo on SQL Fiddle)
SELECT
A.*,
CASE WHEN (A.TotalItems IS NULL OR B.TotalItems IS NULL OR B.TotalItems=0) THEN 0 ELSE
(A.TotalItems - B.TotalItems)*100/B.TotalItems END AS ItemDiff,
CASE WHEN (A.TotalCost IS NULL OR B.TotalCost IS NULL OR B.TotalCost=0) THEN 0 ELSE
(A.TotalCost - B.TotalCost)*100/B.TotalCost END AS CostDiff
FROM (SELECT
substr(date_dim.year,1,4) year,
item_dim.item_type,
SUM(fact.total_item)TotalItems,
SUM(fact.total_cost) AS TotalCost
FROM fact
INNER JOIN date_dim
ON fact.date_key = date_dim.date_key
INNER JOIN item_dim
ON fact.item_key = item_dim.item_key
WHERE date_dim.year BETWEEN 2009 AND 2011
GROUP BY date_dim.year,
item_dim.item_type) A LEFT JOIN (SELECT
substr(date_dim.year,1,4) year,
item_dim.item_type,
SUM(fact.total_item)TotalItems,
SUM(fact.total_cost) AS TotalCost
FROM fact
INNER JOIN date_dim
ON fact.date_key = date_dim.date_key
INNER JOIN item_dim
ON fact.item_key = item_dim.item_key
WHERE date_dim.year BETWEEN 2009 AND 2011
GROUP BY date_dim.year,
item_dim.item_type) B
ON A.YEAR=(B.YEAR+1) AND A.ITEM_TYPE=B.ITEM_TYPE;