2017-04-23 134 views
3

我是MDX查詢的新手,並且在SSAS中工作。我有兩個適合工作的查詢,但我希望他們的輸出組合在一個單一的結果。這兩個查詢在由where子句選擇的城市中有所不同,但它們都來自同一個立方體並使用相同的度量。MDX:結合不同標準的輸出

A_to_B:供應商城市A到消費者B市

SELECT { [Measures].[Quantity - Transactions] } ON COLUMNS, 
     { [Tb Product].[Name].[Name].ALLMEMBERS } ON ROWS 
FROM [Cube] 
WHERE ([Tb Supplier].[City].&[A], 
     [Tb Consumer].[City].&[B]) 

B_to_A:供應商B城到消費城市一個

SELECT { [Measures].[Quantity - Transactions] } ON COLUMNS, 
     { [Tb Product].[Name].[Name].ALLMEMBERS } ON ROWS 
FROM [Cube] 
WHERE ([Tb Supplier].[City].&[B], 
     [Tb Consumer].[City].&[A]) 

是否有這些查詢的輸出方式按照產品並排生產,像這樣?在SQL中,我會使用FULL OUTER JOIN,但我無法弄清MDX中的等價物。

 
|   | A_to_B | B_to_A | 
| ProductA |  10 |  2 | 
| ProductB | 100 |  0 | 
| ProductC |  0 |  99 | 

回答

3

只需將你在哪裏報表列:

With 

Member [Tb Supplier].[City].[B2A] as 
Aggregate([Tb Supplier].[City].&[B], [Tb Consumer].[City].&[A]) 

Member [Tb Supplier].[City].[A2B] as 
Aggregate([Tb Supplier].[City].&[A], [Tb Consumer].[City].&[B]) 

Select 
    {[Tb Supplier].[City].[A2B],[Tb Supplier].[City].[B2A]} on 0 
From [Cube] 
Where ([Measures].[Quantity - Transactions]) 
+1

如果您還將'[Measures]。[Quantity - Transactions]'移到WHERE子句中,它會更整齊嗎? – whytheq

+0

完美無缺。 ;) –

+0

編輯之後是否「完美」? ;) – whytheq

1

我測試了這個劇本,它拋出一個異常:

Select 
{[Measures].[Quantity - Transactions]} * 
{ 
    ([Tb Supplier].[City].&[B], [Tb Consumer].[City].&[A]), 
    ([Tb Supplier].[City].&[A], [Tb Consumer].[City].&[B]) 
} on 0, 
{[Tb Product].[Name].[Name].AllMembers} on 1 
From [Cube] 

你可能爲了團結兩個元創建計算成員:

SELECT 
    {[Measures].[Internet Sales Amount]} 
    * 
    { 
     {[Geography].[Geography].[Country].&[United States] * [Product].[Category].&[1]} 
    ,{[Geography].[Geography].[Country].&[France] * [Product].[Category].&[3]} 
    } ON 0 
,{[Date].[Calendar].[Date].&[20070801]} ON 1 
FROM [Adventure Works]; 

此消息:

查詢(5,7)函數需要參數爲1的 參數的元組集表達式。使用了一個字符串或數字表達式。

這是因爲您需要使交叉連接的任一側具體爲一個集合 - 沒有額外的括號它不知道這一點並引發異常。

所以這是腳本的「完美」的版本:

SELECT 
    {[Measures].[Internet Sales Amount]} 
    * 
    { 
     {[Geography].[Geography].[Country].&[United States]} * {[Product].[Category].&[1]} 
    ,{[Geography].[Geography].[Country].&[France]} * {[Product].[Category].&[3]} 
    } ON 0 
,{[Date].[Calendar].[Date].&[20070801]} ON 1 
FROM [Adventure Works]; 

我寧願措施移動到WHERE子句,也擺脫一些多餘的括號:

SELECT 
    { 
     {[Geography].[Geography].[Country].&[United States]} 
    * 
     {[Product].[Category].&[1]} 
    , 
     {[Geography].[Geography].[Country].&[France]} 
    * 
     {[Product].[Category].&[3]} 
    } ON 0 
,[Date].[Calendar].[Date].&[20070801] ON 1 
FROM [Adventure Works] 
WHERE 
    [Measures].[Internet Sales Amount]; 

翻譯成你的立方體:

SELECT 
    { 
    {[Tb Supplier].[City].&[B]} * {[Tb Consumer].[City].&[A]} 
    , 
    {[Tb Supplier].[City].&[A]} * {[Tb Consumer].[City].&[B]} 
    } ON 0 
,[Tb Product].[Name].[Name].ALLMEMBERS ON 1 
FROM [Cube] 
WHERE 
    [Measures].[Quantity - Transactions]; 
+0

啊,對了,謝謝你的改正!我已經更新了我的答案。也可以使用[*] [([D1],[H1]。[M1],[D2],[H2]。[M2]),([D1],[H1]。[M2],[D2] ,[H 2] [M1])}。 –

0

建立在別人,這裏是一個零代替空s輸出。

With 

Member [Tb Supplier].[City].[B2A] as 
Aggregate([Tb Supplier].[City].&[B], [Tb Consumer].[City].&[A]) 

Member [Tb Supplier].[City].[A2B] as 
Aggregate([Tb Supplier].[City].&[A], [Tb Consumer].[City].&[B]) 

Member [Measures].[Quantity_Transactions] as 
    Iif(IsEmpty([Measures].[Quantity - Transactions]), 
    0, 
    [Measures].[Quantity - Transactions]) 

SELECT 
    { [Measures].[Quantity_Transactions] } * 
    { [Tb Supplier].[City].[B2A], 
     [Tb Supplier].[City].[A2B] } on COLUMNS 
    , [Tb Product].[Name].[Name].ALLMEMBERS ON ROWS 
FROM [Cube] 
相關問題