基于条件的 MDX 查询到数据透视表
Posted
技术标签:
【中文标题】基于条件的 MDX 查询到数据透视表【英文标题】:MDX query to pivot table based on condition 【发布时间】:2015-06-25 10:32:26 【问题描述】:我正在尝试为数据透视表编写 MDX 查询。
RDBMS 中的类似查询是这样的:
SELECT stats_Date
,ISNULL(SUM(clicks), 0) AS clicks
,ISNULL(SUM(CASE WHEN ad_type IN (1,3) THEN clicks END), 0) AS keyword_clicks
,ISNULL(SUM(CASE WHEN ad_type IN (2,3) THEN clicks END), 0) AS direct_clicks
FROM STATS_TABLE (NOLOCK)
WHERE stats_Date BETWEEN '2015-06-01' AND '2015-06-30'
GROUP BY stats_Date
我有两个维度 [DIM TIME]
和 [DIM AD TYPE]
我已经尝试过以下 MDX 查询:
WITH
MEMBER [Measures].[Clicks Keyword] AS
IIF
(
[DIM AD TYPE].[Ad Type].CurrentMember IS [DIM AD TYPE].[Ad Type].&[1]
,[Measures].[clicks]
,0
)
SELECT
[Measures].[Clicks]
,[Measures].[Clicks Keyword]
ON COLUMNS
,
[DIM TIME].[CalendarHierarchy].[Date]*[DIM AD TYPE].[Ad Type].[Ad Type]
ON ROWS
FROM [CM_STATS_CUBE]
WHERE ([DIM TIME].[Month].&[201506]:[DIM TIME].[Month].&[201506])
此 MDX 查询的示例输出如下所示:
Clicks Clicks Keyword
20150501 Invalid (null) 0
20150501 unknown 200 0
20150501 Keyword 500 0
20150501 Ads 300 300
20150502 Invalid (null) 0
20150502 unknown 400 0
20150502 Keyword 600 0
20150502 Ads 500 500
但我只想按 stats_date 分组,预期的输出是:
Clicks Clicks Keyword
20150501 1000 300
20150502 1500 500
在 [Adventure Works] 立方体数据库中进行测试的类似示例:
WITH
MEMBER [Measures].[Internet Sales Amount US] AS
IIF( [Customer].[Customer Geography].CurrentMember IS [Customer].[Customer Geography].[Country].&[United States]
,[Measures].[Internet Sales Amount]
,NULL
)
SELECT
[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales Amount US]
ON 0
,NON EMPTY[Date].[Calendar].[Date] ON 1
FROM [Adventure Works]
WHERE [Date].[Date].&[20050701]:[Date].[Date].&[20050702]
【问题讨论】:
那个例子不好。它给出了错误。 【参考方案1】:您无需为交叉加入而烦恼[DIM TIME].[CalendarHierarchy].[Date]*[DIM AD TYPE].[Ad Type].[Ad Type]
WITH
MEMBER [Measures].[Clicks Keyword] AS
IIF
(
[DIM AD TYPE].[Ad Type].CurrentMember IS [DIM AD TYPE].[Ad Type].&[1]
,[Measures].[clicks]
,0
)
SELECT
[Measures].[Clicks]
,[Measures].[Clicks Keyword]
ON COLUMNS
,[DIM TIME].[CalendarHierarchy].[Date] ON ROWS
FROM [CM_STATS_CUBE]
WHERE
[DIM TIME].[Month].&[201506] : [DIM TIME].[Month].&[201506];
我还建议在您的 IIF
函数中使用 null
而不是 0
- 这应该可以整理结果并加快处理速度:
WITH
MEMBER [Measures].[Clicks Keyword] AS
IIF
(
[DIM AD TYPE].[Ad Type].CurrentMember IS [DIM AD TYPE].[Ad Type].&[1]
,[Measures].[clicks]
,null //<<<<<<<<<<<<<<<<< better to use null rather than 0
)
SELECT
[Measures].[Clicks]
,[Measures].[Clicks Keyword]
ON COLUMNS
, NON EMPTY //<<<<<<<<<<<<<<<<< now if Clicks and Clicks Keyword are both null the respective row will be excluded
[DIM TIME].[CalendarHierarchy].[Date] ON ROWS
FROM [CM_STATS_CUBE]
WHERE
[DIM TIME].[Month].&[201506] : [DIM TIME].[Month].&[201506];
编辑
我没有详细阅读您的脚本 - 抱歉。你可以只聚合一组两个元组:
WITH
MEMBER [Measures].[Clicks Keyword] AS
Sum
(
([DIM AD TYPE].[Ad Type].&[1],[Measures].[clicks])
,([DIM AD TYPE].[Ad Type].&[3],[Measures].[clicks])
)
SELECT
[Measures].[Clicks]
,[Measures].[Clicks Keyword]
ON COLUMNS
, NON EMPTY //<<<<<<<<<<<<<<<<< now if Clicks and Clicks Keyword are both null the respective row will be excluded
[DIM TIME].[CalendarHierarchy].[Date] ON ROWS
FROM [CM_STATS_CUBE]
WHERE
[DIM TIME].[Month].&[201506] : [DIM TIME].[Month].&[201506];
您发布的AdvWrks
示例只是一个元组:
WITH
MEMBER [Measures].[Internet Sales Amount US] AS
(
[Customer].[Customer Geography].[Country].&[United States]
,[Measures].[Internet Sales Amount]
)
SELECT
[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales Amount US]
ON 0
,NON EMPTY[Date].[Calendar].[Date] ON 1
FROM [Adventure Works]
WHERE [Date].[Date].&[20050701]:[Date].[Date].&[20050702]
如果您想在加拿大添加,那么似乎有三种可行的选择:
1.
WITH
MEMBER [Measures].[Internet Sales Amount US & Canada] AS
(
[Customer].[Customer Geography].[Country].&[United States]
,[Measures].[Internet Sales Amount]
)
+
(
[Customer].[Customer Geography].[Country].&[Canada]
,[Measures].[Internet Sales Amount]
)
2.
WITH
MEMBER [Measures].[Internet Sales Amount US & Canada] AS
Aggregate
(
[Customer].[Customer Geography].[Country].&[United States]
,[Customer].[Customer Geography].[Country].&[Canada]
,[Measures].[Internet Sales Amount]
)
3。 (切换到总和)
WITH
MEMBER [Measures].[Internet Sales Amount US & Canada] AS
Sum
(
(
[Customer].[Customer Geography].[Country].&[Canada]
,[Measures].[Internet Sales Amount]
)
,(
[Customer].[Customer Geography].[Country].&[United States]
,[Measures].[Internet Sales Amount]
)
)
【讨论】:
谢谢.. 但我试过了,输出列中全是零或空值。 有人可以尝试在[Adventure Works]
测试数据库上显示类似的查询吗?
@Omesh 请进行我向您的计算成员[Measures].[Clicks Keyword]
建议的更改,即将null
作为分支之一。然后还要注意我已将关键字NON EMPTY
添加到您的ON ROWS
声明中
也试过了,但还是没有成功。我在[Adventure Works]
数据库中编辑了类似示例的问题。您可以在此测试此查询的输出。
[DIM AD TYPE].[Ad Type].CurrentMember
将如何工作?没有设置范围。【参考方案2】:
试试这个:
WITH
MEMBER [Measures].[Clicks Keyword] AS
AGGREGATE([DIM AD TYPE].[Ad Type].&[1], [DIM AD TYPE].[Ad Type].&[3], [Measures].[Clicks])
SELECT NON EMPTY
[Measures].[Clicks]
,[Measures].[Clicks Keyword]
ON COLUMNS
, NON EMPTY
[DIM TIME].[CalendarHierarchy].[Date] ON ROWS
FROM [CM_STATS_CUBE]
WHERE
([DIM TIME].[Month].&[201506] : [DIM TIME].[Month].&[201506]);
【讨论】:
我们怎样才能包含这个条件ad_type IN (1,3)
谢谢!也试过了,但似乎没有用。
它有什么问题?以上是关于基于条件的 MDX 查询到数据透视表的主要内容,如果未能解决你的问题,请参考以下文章