如何模拟SQL减去mdx中两个不同查询的结果

时间:2016-01-03 18:09:05

标签: mdx mondrian

我想在日期之间进行趋势分析。对于实例当前日期 - 30天 30-60天等等。下面是可比较的SQL查询的片段,但我想在MDX中做同样的事情。

SQL

{
  "name": "angular-seed",
  "private": true,
  "version": "0.0.0",
  "description": "A starter project for AngularJS",
  "repository": "https://github.com/angular/angular-seed",
  "license": "MIT",
  "devDependencies": {
    "bower": "^1.3.1",
    "grunt": "^0.4.5",
    "grunt-contrib-jshint": "^0.11.2",
    "grunt-contrib-watch": "^0.6.1",
    "http-server": "^0.6.1",
    "jasmine-core": "^2.3.4",
    "karma": "~0.12",
    "karma-chrome-launcher": "^0.1.12",
    "karma-firefox-launcher": "^0.1.6",
    "karma-jasmine": "^0.3.5",
    "karma-junit-reporter": "^0.2.2",
    "protractor": "^2.1.0",
    "shelljs": "^0.2.6"
  },
  "scripts": {
    "postinstall": "bower install",
    "prestart": "npm install",

    "start": "lr-http-server",
    "pretest": "npm install",
    "test": "karma start karma.conf.js",
    "test-single-run": "karma start karma.conf.js  --single-run",
    "preupdate-webdriver": "npm install",
    "update-webdriver": "webdriver-manager update",
    "preprotractor": "npm run update-webdriver",
    "protractor": "protractor e2e-tests/protractor.conf.js",
    "update-index-async": "node -e \"require('shelljs/global'); sed('-i', /\\/\\/@@NG_LOADER_START@@[\\s\\S]*\\/\\/@@NG_LOADER_END@@/, '//@@NG_LOADER_START@@\\n' + sed(/sourceMappingURL=angular-loader.min.js.map/,'sourceMappingURL=bower_components/angular-loader/angular-loader.min.js.map','app/bower_components/angular-loader/angular-loader.min.js') + '\\n//@@NG_LOADER_END@@', 'app/index-async.html');\""
  }
}

MDX:

SELECT
ROUND
(
    (
        (
            (
            SELECT
                SUM(del_pri_impr)
            FROM
                reporting.so_sli_calc_val a,
                reporting.user_group_tenant b,
                reporting.salesorder c
            WHERE
                created_on BETWEEN DATE(now()-30) AND DATE(now())
            )

            -

            (            
            SELECT
                SUM(del_pri_impr)
            FROM
                reporting.so_sli_calc_val a,
                reporting.user_group_tenant b,
                reporting.salesorder c
            WHERE
                created_on BETWEEN DATE(now()-60) AND DATE(now()-30)             
            )
        )

        /

        (
                   SELECT
                       SUM(del_pri_impr)
                   FROM
                       reporting.so_sli_calc_val a,
                       reporting.user_group_tenant b,
                     reporting.salesorder c
                 WHERE
                     created_on BETWEEN DATE(now()-60) AND DATE(now()-30)
        ) *100
    )

,
0
) AS trend

截至目前,我已经对参数的日期进行了硬编码。 我在创建第二组时遇到困难,以及如何在MDX中进行两组之间的减法。

2 个答案:

答案 0 :(得分:2)

您已经掌握了如何获取与&#34相对应的日期集的逻辑;从现在开始的30天"和#34;从现在起60天到最后30天"。所以,我将跳过那一部分。

注意 - 在构建这些集合时,您必须使用参数值。

您要做的是首先找到与这些日期对应的值,然后对它们执行操作。

你可以这样继续 -

WITH
  SET [~FILTER] AS
    {[Created_Date.Created_Hir].[Created_On].[2014-04-01]:[Created_Date.Created_Hir].[Created_On].[2014-04-30]}
  SET [~ROWS] AS
  {[Sales Order Attributes SO.Sales_order].[Sales Order ID].Members}
  SET [Last30Days] AS
  ... 
  SET [Last60ToLast30Days] AS
  ...
  MEMBER [~Last30Days - Now] AS
  Aggregate   
  (
      [Last30Days],
      [Measures].[SomeMeasure]
  )

  MEMBER [~Last60Days - Last30Days] AS
  Aggregate
  (
      [Last60ToLast30Days],
      [Measures].[SomeMeasure]
  )


  MEMBER [~Measure] AS
  ([~Last30Days - Now]-[~Last60Days - Last30Days] )/([~Last60Days - Last30Days] * 100), format_string = '#,##0'

  SELECT
   NON EMPTY {
                [Measures].[CONT_AMT_GROSS], 
                [Measures].[CONT_AMT_NET],
                [~Measure]
             } ON COLUMNS,
   NON EMPTY [~ROWS] ON ROWS
   FROM [SALES_ORDER]

Format_String负责四舍五入。

答案 1 :(得分:1)

不确定我是否完全同意Sourav的答案,因为我认为需要某种形式的汇总;创建带有集合的元组可能会引发异常。

这是一个针对AdvWrks的简单模型,经过测试并将为您做减法:

WITH 
  SET [Set1] AS 
      [Date].[Calendar].[Date].&[20060301]
    : 
      [Date].[Calendar].[Date].&[20070308] 
  SET [Set2] AS 
      [Date].[Calendar].[Date].&[20070308]
    : 
      [Date].[Calendar].[Date].&[20080315]
  MEMBER [Date].[Calendar].[All].[Set1Agg] AS 
     aggregate([Set1])
  MEMBER [Date].[Calendar].[All].[Set2Agg] AS 
     aggregate([Set2])

  MEMBER [Date].[Calendar].[All].[x] AS 
    (
      [Date].[Calendar].[All].[Set1Agg]
     ,[Measures].[Internet Sales Amount]
    ) 
  MEMBER [Date].[Calendar].[All].[y] AS 
    (
      [Date].[Calendar].[All].[Set2Agg]
     ,[Measures].[Internet Sales Amount]
    ) 
  MEMBER [Date].[Calendar].[All].[x-y] AS 
    [Date].[Calendar].[All].[x] - [Date].[Calendar].[All].[y] 
SELECT 
  {
    [Date].[Calendar].[All].[x]
   ,[Date].[Calendar].[All].[y]
   ,[Date].[Calendar].[All].[x-y]
  } ON 0
 ,[Product].[Category].[Category] ON 1
FROM [Adventure Works];

反映您的代码可能如下所示:

WITH 
  SET [Set1] AS 
      [Created_Date.Created_Hir].[Created_On].[2014-04-01]
    : 
      [Created_Date.Created_Hir].[Created_On].[2014-04-30] 
  SET [Set2] AS 
      [Created_Date.Created_Hir].[Created_On].[2014-03-01]
    : 
      [Created_Date.Created_Hir].[Created_On].[2014-03-31] 
  MEMBER [Created_Date.Created_Hir].[All].[Set1Agg] AS 
    Aggregate([Set1]) 
  MEMBER [Created_Date.Created_Hir].[All].[Set2Agg] AS 
    Aggregate([Set2]) 
  MEMBER [Measures].[~Last30Days - Now] AS 
    (
      [Created_Date.Created_Hir].[All].[Set1Agg]
     ,[Measures].[SomeMeasure]
    ) 
  MEMBER [Measures].[~Last60Days - Last30Days] AS 
    (
      [Created_Date.Created_Hir].[All].[Set2Agg]
     ,[Measures].[SomeMeasure]
    ) 
  MEMBER [Measures].[~Measure] AS 

        ([Measures].[~Last30Days - Now] - [Measures].[~Last60Days - Last30Days])
      / 
        [Measures].[~Last60Days - Last30Days]
    * 100 
   ,format_string = '#,##0' 
  SET [~ROWS] AS 
    {
      [Sales Order Attributes SO.Sales_order].[Sales Order ID].MEMBERS
    } 
SELECT 
  NON EMPTY 
    {
      [Measures].[CONT_AMT_GROSS]
     ,[Measures].[CONT_AMT_NET]
     ,[Measures].[~Measure]
    } ON COLUMNS
 ,NON EMPTY 
    [~ROWS] ON ROWS
FROM [SALES_ORDER]
WHERE 
  [~FILTER];