## MySql Server 5.5,数据库引擎 MyIsam,表 fact_transaction 在 date_key、time_key、unit_cost_price、unit_retail_price(组合键)和 fact_stockout_sales 上索引,不包括 time_key。##
询问
SELECT
t.Level, t.Name, t.KeyValue,
ROUND( (SUM(t.Gross)/SUM(t.Revenue))*100, 2 ) AS Value,
ROUND( (SUM(t.adjustedGross)/SUM(t.adjustedRevenue))*100, 2 ) AS adjustedValue,
t.dataType AS dataType
FROM
(SELECT "item" AS Level, ds.product_name AS Name, ds.product_id AS KeyValue,
SUM(ft.gross_profit) AS Gross,
SUM(ft.selling_amount) AS Revenue,
SUM(ft.adjusted_gross_profit) AS adjustedGross,
SUM(ft.adjusted_selling_amount) AS adjustedRevenue,
"%" AS dataType
FROM fact_transaction AS ft
JOIN dim_sku AS ds ON ft.sku_key = ds.sku_key
WHERE ft.date_key BETWEEN 20080215 AND 20130107
AND ft.time_key BETWEEN 100 AND 235900
AND ft.unit_cost_price BETWEEN 0 AND 1333
AND ft.unit_retail_price BETWEEN 0 AND 16500
AND ft.store_key IN ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16")
AND ds.product_id IN (1312009,1312007,... Huge List say 30000)
AND ds.category IN ("Male","Female","Unisex")
AND ft.day_of_week IN ("1","2","3","4","5","6","7")
AND ds.collection_name IN ("Base","SS12","AW12")
GROUP BY ds.product_id
UNION
SELECT "item" AS Level, ds.product_name AS Name, ds.product_id AS KeyValue,
SUM(ft.gross_profit) AS Gross,
SUM(ft.selling_amount) AS Revenue,
SUM(ft.adjusted_gross_profit) AS adjustedGross,
SUM(ft.adjusted_selling_amount) AS adjustedRevenue,
"%" AS dataType
FROM fact_stockout_sales AS ft
JOIN dim_sku AS ds ON ft.sku_key = ds.sku_key
WHERE ft.date_key BETWEEN 20080215 AND 20130107
AND ft.unit_cost_price BETWEEN 0 AND 1333
AND ft.unit_retail_price BETWEEN 0 AND 16500
AND ft.store_key IN ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16")
AND ds.product_id IN (1312009,1312007,.. Huge List say 30000)
AND ds.category IN ("Male","Female","Unisex")
AND ft.day_of_week IN ("1","2","3","4","5","6","7")
AND ds.collection_name IN ("Base","SS12","AW12")
GROUP BY ds.product_id) AS t
GROUP BY t.KeyValue