MDX 计算共同成员 - 存在替代
Posted
技术标签:
【中文标题】MDX 计算共同成员 - 存在替代【英文标题】:MDX to count common members - EXISTS alternative 【发布时间】:2015-01-10 10:55:23 【问题描述】:让我以 [Adventure Works] 立方体为例来描述问题。
按照 MDX 返回计数 17473
SELECT NON EMPTY [Measures].[Internet Order Count] ON COLUMNS,
[Internet Sales Order Details].[Sales Order Number] on ROWS
FROM [Adventure Works])
WHERE ( [Sales Reason].[Sales Reason].&[1] -- price
以下返回计数为 3515
SELECT NON EMPTY [Measures].[Internet Order Count] ON COLUMNS ,
[Internet Sales Order Details].[Sales Order Number] on ROWS
FROM [Adventure Works]
WHERE ( [Sales Reason].[Sales Reason].&[2]) -- on promotion
我想统计一下[Sales Reason]中常见的[Sales Order Number]。&1和[Sales Reason]。&[2]
SQL 等效项是:
select count(distinct f.SalesOrderNumber)
from FactInternetSales f
join FactInternetSalesReason fs
on f.SalesOrderNumber = fs.SalesOrderNumber and f.SalesOrderLineNumber = fs.SalesOrderLineNumber
where fs.SalesReasonKey = 1 and fs.SalesOrderNumber in
(select SalesOrderNumber from FactInternetSalesReason fs1 where fs1.SalesReasonKey = 2)
-- sales reason 1 = 17473
-- sales reason 2 = 3515
-- common 1689
我使用以下 mdx 得到了常见计数:
WITH MEMBER [Measures].[common] AS count
( exists ( exists ([Internet Sales Order Details].[Sales Order Number].[Sales Order Number].Members,
[Sales Reason].[Sales Reason].&[1],"Internet Orders"
),
[Sales Reason].[Sales Reason].&[2],"Internet Orders"
)
)
SELECT NON EMPTY [Measures].[common] ON COLUMNS
FROM [Adventure Works]
-- 1689
但是对于我的要求,使用 EXISTS 相当慢。请提出替代方案。
另请参阅相关主题here
谢谢
【问题讨论】:
【参考方案1】:请尝试将这两个原因作为一个集合添加到WHERE
子句中:
SELECT
NON EMPTY
[Measures].[Internet Order Count] ON COLUMNS
,[Internet Sales Order Details].[Sales Order Number] ON ROWS
FROM [Adventure Works]
WHERE
[Sales Reason].[Sales Reason].&[2]
,[Sales Reason].[Sales Reason].&[1]
;
这里有一个运行速度更快的替代方案,只看常见的命令,不使用EXISTS
函数:
WITH
SET [AllOrders] AS
[Internet Sales Order Details].[Sales Order Number].[Sales Order Number].MEMBERS
SET [OrdersIntersection] AS
Intersect
(
NonEmpty
(
[AllOrders]
,
(
[Sales Reason].[Sales Reason].&[1]
,[Measures].[Internet Order Count]
)
)
,NonEmpty
(
[AllOrders]
,
(
[Sales Reason].[Sales Reason].&[2]
,[Measures].[Internet Order Count]
)
)
)
MEMBER [Measures].[commonCount] AS
[OrdersIntersection].Count
SELECT
//NON EMPTY //<<not needed
[Measures].[commonCount] ON COLUMNS
FROM [Adventure Works];
【讨论】:
我需要“销售原因”1 和 2 中常见的“互联网订单数”,而不是总数。 @PrakashGautam - 我误解了你的问题。我在我的帖子中添加了一个替代方案。您的exists
脚本在我的机器上运行时间为 1.1 秒,而我的交集版本运行时间为 0.8 秒。
感谢@whytheq,Intersect 的运行速度确实比 Exists 快。以上是关于MDX 计算共同成员 - 存在替代的主要内容,如果未能解决你的问题,请参考以下文章