MDX for Zero months in last 12 = 过去 12 个月滚动窗口中的零交易月数

标签 mdx ssas-2008

我正在处理 MDx 查询,以计算从今天开始的过去 12 个月内每种产品的零交易数量。为 Adventure 准备的 MDX 查询工作正常,但我在 Acutal 多维数据集上运行的第二个查询正常工作。

能否请您帮助我纠正查询或获得正确结果(如第一个示例查询)所需的任何过滤器?

关于 Adventure Works 的第一个示例查询:(工作正常)

WITH Member [Measures].[Months With Zero Sales] AS 
COUNT(
    FILTER(
        DESCENDANTS(
        {[Date].[Calendar].[Month].&[2008]&[1].LAG(12):[Date].[Calendar].[Month].&[2008]&[1]}, [Date].[Calendar].[Month]) , 
[Measures].[Sales Amount] = 0 ) )
SELECT {[Measures].[Months With Zero Sales]} ON 0, 
[Product].[Product Model Lines].[Product Line].Members on 1
FROM [Adventure Works];

Product Lines  Months With Zero Sales]
-----------    -----------------
Accessory      0
Componenets    0
Mountain       0
Road           0
Touring        6  

我的多维数据集上的第二个查询:

WITH Member [Measures].[Zero Months in last 12] AS 
COUNT(
      FILTER(
            DESCENDANTS(
            {[Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1].LAG(12):
            [Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1]}, 
            [Settlement Date].[Calendar].[Settlement Calendar Month]),
            [Measures].[Transaction Count] = 0 ) )
SELECT {[Measures].[Transaction Count] ,[Measures].[Zero Months in last 12]} ON 0, 
[Terminal].[terminal ID].members on 1
FROM [cubetxn]

I'm expecting if non zero where "transaction count" is (null), even if there is transaction count not null, there could be chances to have one month with empty tuples.

Can you please correct my query or someone can give me sample query which should able to give me total zero month txn count for last 12 month of each product.

Terminal ID    Transaction Count      Zero Months in last 12
----------     -----------------      -------------------------
All Terminals   504,112,053            0
Q1001             138,832              0
Q1002             (null)               0
Q1003              88,800              0
Q1004             (null)               0

最佳答案

第一个评论是,在进行计数/过滤时,这是一种更快的模式:

WITH 
  MEMBER [Measures].[Months With Zero Sales] AS 
    Sum
    (
      Descendants
      (
        {
            [Date].[Calendar].[Month].&[2008]&[1].Lag(12)
          : 
            [Date].[Calendar].[Month].&[2008]&[1]
        }
       ,[Date].[Calendar].[Month]
      )
     ,IIF
      (
        [Measures].[Sales Amount] = 0
       ,1
       ,null
      )
    ) 
SELECT 
  {[Measures].[Months With Zero Sales]} ON 0
 ,[Product].[Product Model Lines].[Product Line].MEMBERS ON 1
FROM [Adventure Works];

基于这种更有效的方法,以下工作是否有效?

WITH 
  MEMBER [Measures].[Zero Months in last 12] AS 
    Sum
    (
      Descendants
      (
        {
            [Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1].Lag(12)
          : 
            [Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1]
        }
       ,[Settlement Date].[Calendar].[Settlement Calendar Month]
      )
     ,IIF
      (
        [Measures].[Transaction Count] = 0
       ,1
       ,0
      )
    ) 
SELECT 
  {
    [Measures].[Transaction Count]
   ,[Measures].[Zero Months in last 12]
  } ON 0
 ,[Terminal].[terminal ID].MEMBERS ON 1
FROM [cubetxn];

如果仍然存在问题,那么为什么不重建您的脚本,以便您可以手动检查预期结果。因此,对于 AdvWrks 脚本,我将运行此脚本,以便我可以实际确认 6 是正确的:

SELECT 
  {[Measures].[Sales Amount]} ON 0
 ,
    Descendants
    (
      {
          [Date].[Calendar].[Month].&[2008]&[1].Lag(12)
        : 
          [Date].[Calendar].[Month].&[2008]&[1]
      }
     ,[Date].[Calendar].[Month]
    )
  * 
    [Product].[Product Model Lines].[Product Line].MEMBERS ON 1
FROM [Adventure Works];

那么针对您的多维数据集,以下返回什么?

SELECT 
  {[Measures].[Transaction Count]} ON 0
 ,
    Descendants
    (
      {
          [Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1].Lag(12)
        : 
          [Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1]
      }
     ,[Settlement Date].[Calendar].[Settlement Calendar Month]
    )
  * 
    [Terminal].[terminal ID].MEMBERS ON 1
FROM [cubetxn];

关于MDX for Zero months in last 12 = 过去 12 个月滚动窗口中的零交易月数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29134144/

相关文章:

MDX新手: How do I filter/except based on an intermediate level which is not unique

sql-server - 累积总和-选择层次结构的部分

r - 在 Cube MDX 中使用 R,如何传递和检索数组?

powershell - 使用 Powershell 创建 SSAS 2008 多维数据集分区?

mdx - 大多数人如何使用 MDX 查询?我应该使用 MDX 结果来驱动 Web UI 吗?

ssas - MDX 中的 NONEMPTY 和 CROSSJOIN 性能和顺序

sql-server - MDX - 检索当前在 where 子句中使用的成员级别

sql - 尝试处理多维数据集,但在模拟信息中出现错误

iis - 通过 HTTP 访问 SSAS

ssas - ssas 立方体中的百万行维度