MDX 表達式學習筆記


Count函數.

Distinct函數.

existing關鍵字.

format_String的值

TopCount(exp1, number, exp2): 返回number個exp1集合內 按照exp2降序排序的集合

image

常用屬性函數

image

使用properties能獲取的特性

image

image

比如

WITH
MEMBER [Measures].[Parent Member Name] AS
    IIF(
        [Product].[Product Categories].CurrentMember.Properties
                  ("Level_Number",TYPED)=0,
        "Not Applicable", 
        [Product].[Product Categories].CurrentMember.Parent.Name
         )
member [Measures].[lvNum] as     [Product].[Product Categories].CurrentMember.Properties
                  ("Level_Number")     
SELECT
    {([Measures].[Parent Member Name]),[Measures].[lvNum]} ON COLUMNS,
    {[Product].[Product Categories].AllMembers} ON ROWS
FROM [Step-by-Step]
image

solve_order指定計算列的執行順序

 

看一個基本的mdx

WITH
MEMBER [Product].[Category].[All Products].[X]AS
([Measures].[Internet Sales Amount],([Product].[Category].[Bikes]))+1
//member [Measures].[totalPecent] as 
//    [Measures].[Internet Sales Amount]/([Product].[Category].[All Products],[Measures].[Internet Sales Amount])
//    ,format_string="percent"
SELECT
{
    ([Date].[Calendar Year].[CY 2003]),
    ([Date].[Calendar Year].[CY 2004])
}*
{
    [Measures].[Internet Sales Amount]
//    ,[Measures].[totalPecent]
}
 ON COLUMNS,
{
[Product].[Category].allmembers
} ON ROWS
FROM[Step-by-Step]
image

如圖. 最后一個是Bike的值+1.  而標准的表達式寫法 是(作用范圍, 度量值).

假設, 我們想知道每個分類的銷售額占總的銷售額的比例, 應該怎么做.  思考. 增加一個數據列, 被rows打爛的每個單元格是 該單元格的度量值除以總的產品的度量值.

member [Measures].[totalPecent] as
    [Measures].[Internet Sales Amount]/([Product].[Category].[All Products],[Measures].[Internet Sales Amount])
    ,format_string="percent"

[Measures].[Internet Sales Amount] 沒有指定作用范圍, 因此會被rows的產品給切開. 如果指定了作用域, 比如

([Product].[Category].[All Products],[Measures].[Internet Sales Amount]),  這樣就限制得到一個所有產品的匯總.   有了這個匯總值, 我們就可以計算每個分類占所有產品銷售量的總額了. 得到計算列如上.  結果如圖

image.

 

看一個例子

image.

這個count函數, 計算的是產品的總數量. 相當於select count(*) from product表. 得到是397個.

這樣理解 由於, 這算是個cube外的度量值, 和任何維度(在這里是分類維度)沒有任何關系, 因此它沒有被分類維度拆開打散.

如果, 我們想得到每個分類的產品個數呢.  使用existing關鍵字. 它能強制在某一個維度區域上(這里是分類)進行計算.

image這樣就不一樣了.  得到我們想要的結果.

 

 

來看看TopCount, 並且理解一下 域 的概念

WITH
    SET [Top 10 Products of 2003]    AS
    TOPCOUNT(
        {[Product].[Product].[Product].Members},
        10,
        ( [Measures].[Internet Sales Amount], [Date].[Calendar Year].[CY 2003])
    )
 
SELECT
    {
        [Measures].[Internet Sales Amount]
    } ON COLUMNS,
    {[Top 10 Products of 2003]} ON ROWS
FROM[Step-by-Step]
WHERE ([Date].[Calendar Year].[CY 2004])
;假設我們現在是沒有where條件的. 得到是下面的這個結果,  是銷量在2003年度前十的產品的總銷售額(是數據庫內所有的總銷售哦, 而不僅僅只是2003年度的. 不過排名確是2003年度的排名). 

image

假如改成這樣image.  這里得到的是. 數據庫銷量前十的產品排序. 和上面明顯不一樣了吧.

image

 

OK. 去掉所有的注視. 得到的是. 2003年度銷量前十的產品, 在2004年度的銷售額..

image看. 有些2003年有銷售的產品. 在2004年就沒有銷售了.

 

如果這個時候. 想增加一列. 顯示這些產品的2003年的銷售, 作為對比的話呢. 我們思考一下. 應該就是一個(度量值, 時間限定)image

得到

image

同理, 我們想計算一下2003年的銷量, 占所有銷量的總額也可以增加兩個計算列

增加如下

member Measures.sales2003OfAll as
        ([Measures].[Internet Sales Amount],[Date].[Calendar Year].[CY 2003])/[Measures].[Internet Sales Amount]
        ,format_string="percent"

 

最終如下

WITH
    SET [Top 10 Products of 2003]    AS
    TOPCOUNT(
        {[Product].[Product].[Product].Members},
        10,
        ( [Measures].[Internet Sales Amount], [Date].[Calendar Year].[CY 2003]
        )
    )
    member Measures.sales2004 as
        ([Measures].[Internet Sales Amount],[Date].[Calendar Year].[CY 2004])
    member Measures.sales2003 as
        ([Measures].[Internet Sales Amount],[Date].[Calendar Year].[CY 2003])
    member Measures.salesAll as
        ([Measures].[Internet Sales Amount])
    member Measures.sales2003OfAll as
//        iif(
        ([Measures].[Internet Sales Amount],[Date].[Calendar Year].[CY 2003])/
        ([Measures].[Internet Sales Amount],[Date].[Calendar Year])
        ,format_string="percent"
SELECT
    {
         Measures.sales2004
        , Measures.sales2003
        ,Measures.salesAll
        ,Measures.sales2003OfAll
    } ON COLUMNS,
    {[Top 10 Products of 2003]} ON ROWS
FROM[Step-by-Step]

 

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM