How to use UNION in MDX

大兔子大兔子 提交于 2019-12-02 09:46:00

MDX doesnt support Union, However there is away around it.

1)You can write a query will show you on columns first "es2" for all values of "Pra atus" and "In" value for name. Then it will display "es1" for " we" value of "Pra atus" and all values of "Name". The columns will be side by side. The query will look as below. All values means the default value.

SELECT NON EMPTY 
{ 
([Measures].[Number of es2],[PracHistory].[Name].&[In],[Prac].[Pra atus].[All]),
([Measures].[Number of es1],[PracHistory].[Name].[All],[Prac].[Pra atus].&[ We])
 } ON COLUMNS, 
NON EMPTY { ([Date].[Year].[Year] * [Date].[Month].[Month] ) } ON ROWS 
FROM [Cube]     
WHERE ( [Date].[Date Hierarchy].[Date].&[20170101] : [Date].[Date Hierarchy].[Date].&[20180101] ) 

Sample for above

SELECT NON EMPTY 
{ 
([Measures].[Internet Sales Amount],[Product].[Category].&[1],[Customer].[Country].[All]),
([Measures].[Internet Order Quantity],[Product].[Category].[All],[Customer].[Country].&[United States])
} ON COLUMNS, 
non empty
{ ([Date].[Calendar Year].[Calendar Year], [Date].[Calendar Quarter of Year].[Calendar Quarter of Year]) } 
ON ROWS 
FROM [Adventure Works]   
WHERE ([Date].[Date].&[20130101]:[Date].[Date].&[20140101]  ) 

2)Now if you want it to Display in a single column, to emulate the behavior of SQL union then you will need a calculated measure that will select the value from one of the two measures .However please note that apart of the Date on rows you will also see "Name" and "Pra atus" on rows. The query will look like below.

with member [Measures].[Number of esunion]
as 
case 
when 
[Product].[Category].currentmember is [Product].[Category].defaultmember then [Measures].[Internet Order Quantity]
when 
[Customer].[Country].currentmember is [Customer].[Country].defaultmember then [Measures].[Internet Sales Amount]
else null
end
SELECT NON EMPTY 
{ 
[Measures].[Number of esunion]
} ON COLUMNS,
non empty
{ 
([PracHistory].[Name].&[In],[Prac].[Pra atus].[All], [Date].[Year].[Year] , [Date].[Month].[Month]) ,
([PracHistory].[Name].[All],[Prac].[Pra atus].&[ We],[Date].[Year].[Year] , [Date].[Month].[Month])
} 
ON ROWS 
FROM [Cube]   
WHERE ([Date].[Date Hierarchy].[Date].&[20170101] : [Date].[Date Hierarchy].[Date].&[20180101]  ) 

Sample for above

with member [Measures].[Number of esunion]
as 
case 
when 
[Product].[Category].currentmember is [Product].[Category].defaultmember then [Measures].[Internet Order Quantity]
when 
[Customer].[Country].currentmember is [Customer].[Country].defaultmember then [Measures].[Internet Sales Amount]
else null
end
SELECT NON EMPTY 
{ 
[Measures].[Number of esunion]
} ON COLUMNS,
non empty
{ 
([Product].[Category].&[1],[Customer].[Country].[All],[Date].[Calendar Year].[Calendar Year], [Date].[Calendar Quarter of Year].[Calendar Quarter of Year]) ,
([Product].[Category].[All],[Customer].[Country].&[United States],[Date].[Calendar Year].[Calendar Year], [Date].[Calendar Quarter of Year].[Calendar Quarter of Year])
} 
ON ROWS 
FROM [Adventure Works]   
WHERE ([Date].[Date].&[20130101]:[Date].[Date].&[20140101]  ) 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!