专注于中国的商业智能

导航

KDT#68 一个简单的交叉探察的SQL例子

交叉探查(Drill Across)指查询多个事实表并将结果合并成一个结果集的查询操作。下面是一个查询销售实际值和预测值的例子。通常的BI工具都支持交叉探查操作。

SELECT Act.Customer, Act.Year, Act.Month, Actual_Amount, Fcst.Forecast_Amount

FROM

--子查询"Act"返回实际值

  (SELECT Customer_Name Customer, Year, Month_Name Month,

  SUM(Sale_Amount) Actual_Amount

  FROM Sales_Facts A

  INNER JOIN Customer C

  ON A.Customer_Key=C.Customer_Key

  INNER JOIN Date D

  ON A.Sales_Date_Key=D.Date_Key

  GROUP BY Customer_Name, Year, Month_Name) Act

INNER JOIN

--子查询"Fcst"返回预测值

  (SELECT Customer_Name Customer, Year, Month_Name Month,

  SUM(Forecast_Amount) Forecast_Amount

  FROM Forecast_Facts F

  INNER JOIN Customer C

  ON F.Customer_Key=C.Customer_Key

  INNER JOIN Date D

  ON F.Sales_Date_Key=D.Date_Key

  GROUP BY Customer_Name, Year, Month_Name) Fcst

--组合返回结果

ON Act.Customer=Fcst.Customer

AND Act.Year=Fcst.Year

AND Act.Month=Fcst.Month

交叉探查能够实现的条件是两个事实表使用的是一致性维度,即客户、日期或其他需要共同使用的维度表必须完全一致。这里再次强调一致性维度的重要性。

posted on 2010-08-05 14:33  李梦蛟  阅读(365)  评论(0编辑  收藏  举报