One of my favourite questions in MDX is the difference between Non Empty and NonEmpty because even though many people use them daily to remove NULLS from their queries, very few understand the working behind it. Many times, I have even got answers like “there is a space between Non and Empty, that is the difference”. The objective of this post is to clearly differentiate between the two.

Let us say my initial query is

SELECT  
  { 
    [Measures].[Hits] 
   ,[Measures].[Subscribers] 
   ,[Measures].[Spam] 
  } ON COLUMNS 
,{ 
    [Geography].[Country].Children 
  } ON ROWS 
FROM [Blog Statistics];

This will give the following output

NON EMPTY

Non Empty is prefixed before the sets defining the axes and is used for removing NULLs. Let us see what happens when we add Non Empty on the Rows axis.

SELECT  
  { 
    [Measures].[Hits] 
   ,[Measures].[Subscribers] 
   ,[Measures].[Spam] 
  } ON COLUMNS 
,NON EMPTY  
    { 
      [Geography].[Country].Children 
    } ON ROWS 
FROM [Blog Statistics];

The output is shown below

You will notice that Chile (CL) has been filtered out while rows like UK, Canada, etc are still there even if they have NULLs for some of the measures. In short, only the rows having NULL for all the members of the set defined in the column axis is filtered out. This is because the Non Empty operator works on the top level of the query. Internally, the sets defined for the axes are generated first and then the tuples having NULL values are removed. Now that we know how NON EMPTY works, it shouldn’t be hard for us to tell the output of the below query

SELECT  
  NON EMPTY  
    { 
      [Measures].[Hits] 
     ,[Measures].[Subscribers] 
     ,[Measures].[Spam] 
    } ON COLUMNS 
,{ 
    [Geography].[Country].Children 
  } ON ROWS 
FROM [Blog Statistics];

The output is shown below

NONEMPTY()

The NonEmpty() returns the set of tuples that are not empty from a specified set, based on the cross product of the specified set with a second set. Suppose we want to see all the measures related to countries which have a non-null value for Subscribers

SELECT  
  { 
    [Measures].[Hits] 
   ,[Measures].[Subscribers] 
   ,[Measures].[Spam] 
  } ON COLUMNS 
,{ 
    NonEmpty 
    ( 
      [Geography].[Country].Children 
     ,[Measures].[Subscribers] 
    ) 
  } ON ROWS 
FROM [Blog Statistics];

This will give the following output

As you can see, the NonEmpty operator takes all the rows having a not NULL value for Subscribers in the rows and then displays all the measures defined in the column axis. Basically what happens internally is that NonEmpty is evaluated when the sets defining the axis are evaluated. So at this point of time, there is no context of the other axes. What I said now can be better understood from the following example

Now, we write the below query

SELECT  
  {[Date].[Month].[March]} ON COLUMNS 
,{ 
    [Geography].[Country].Children 
  } ON ROWS 
FROM [Blog Statistics] 
WHERE  
  [Measures].[Hits];

Output is given below

Think for a while and predict which all rows would be returned when the NonEmpty operator is applied on the rows

SELECT  
  {[Date].[Month].[March]} ON COLUMNS 
,{ 
    NonEmpty([Geography].[Country].Children) 
  } ON ROWS 
FROM [Blog Statistics] 
WHERE  
  [Measures].[Hits];

If you guessed just IN, US, GB and AU, please go back and read once again. If you replied All rows except Chile, full marks to you, you have been an attentive reader. The reason is because NonEmpty is evaluated when the set defining the axis is evaluated (here, Country) and at that point of time, NonEmpty is evaluated for each member of the country against the default member of the Date dimension (which would be ALL generally). As you can see, we already have values for CA and AP for other months and hence they will not be filtered out.

Optimizing Non Empty by using NonEmpty

Ok, now you know how Non Empty and NonEmpty works internally and we can apply this knowledge to optimize our queries. Suppose there is a complex logic in our axes like finding all the countries that have 30 or more hits in any month. The query is given below

SELECT  
  {[Measures].[Hits]} ON COLUMNS 
,{ 
    Filter 
    ( 
        [Geography].[Country].Children 
      *  
        [Date].[Month].Children 
     , 
      [Measures].[Hits] > 30 
    ) 
  } ON ROWS 
FROM [Blog Statistics];

Now my time dimension will have 10 years of data, which means around 120 (10*12) members for the month attribute and my country attribute may have let’s say, 100 members. Now even though I just have 3 months of data for 10 countries for hits, the filter function will need to go through all the combinations of country and month (120*100 combinations). Instead of that, we can just use the NonEmpty operator and bring down the combinations to less than 30 (3 months*10 countries) by using the below query

SELECT  
  {[Measures].[Hits]} ON COLUMNS 
,{ 
    Filter 
    ( 
      NonEmpty 
      ( 
          [Geography].[Country].Children 
        *  
          [Date].[Month].Children 
       ,[Measures].[Hits] 
      ) 
     , 
      [Measures].[Hits] > 30 
    ) 
  } ON ROWS 
FROM [Blog Statistics];

 
 

NonEmpty和Non Empty的区别[转]的更多相关文章

  1. 【转载】NonEmpty和Non Empty的区别

    转载来源:http://www.ssas-info.com/analysis-services-articles/50-mdx/2196-mdx-non-empty-vs-nonempty One o ...

  2. [JS][jQuery]清空元素html("")、innerHTML="" 与 empty()的区别 、remove()区别

    清空元素html("").innerHTML="" 与 empty()的区别 一.清空元素的区别     1.错误做法一:           $(" ...

  3. EL表达式中null和empty的区别

    下面通过一个例子看看看null和empty的区别,建立一个test.jsp文件,内容如下: <%@page pageEncoding="utf-8" %> name:$ ...

  4. php 中 isset 和empty 的区别

    昨天终于开始学习php了,这个对于我来说听着很熟悉,但是学起来很陌生的东西,尤其是课上能听明白 但是真正写起了手生,都不知道手该往哪里放了,天哪~~~ 其中课上有讲到 isset和empty的区别,现 ...

  5. C#中string.Empty ,"" , null 区别

    引言 String类型作为使用最频繁的类型之一,相信大家都非常熟悉,对于string赋予空值,通常有以下三种方式: String str1=null; String str2=””; String s ...

  6. PHP isset()与empty()的区别详解

    通过对PHP语言的学习,应该知道它是基于函数的一款HTML脚本语言. 庞大的函数库支持着PHP语言功能的实现. 有关PHP函数isset()与empty()的相关用法. PHP的isset()函数 一 ...

  7. isset() 与 empty() 的区别

    PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...

  8. isset与empty 的区别

    isset()与empty()函数的区别,isset()只需要验证一个值是否存在: 而empty()不但需验证这个值是否存在,还需检验它的值是否非空和非0: 注:isset()只检验一个变量是否已经设 ...

  9. phpisset()和empty()函数区别

    PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...

随机推荐

  1. RHCE ext3文件系统故障一例

    好久没来了,博客长草了,我来除除草. 给我分了两人,一个统招,一个Java两年开发经验的社招,让我这从工具平台运维往Python开发方向转的工作是举步维艰啊~ 领导看人还是真特么的不准,希望今年招聘的 ...

  2. webstrom11 激活,webstorm 2016.1激活

    http://15.idea.lanyus.com/  webstorm11注册激活,你值得拥有 webstorm 2016.1 最新激活方式:http://blog.lanyus.com/archi ...

  3. SRM 502 DIV1 500pt(DP)

    题目简述 给定比赛时间T和n个题目,你可以在任意时间提交题目,每个题目有一个初始分数maxPoints[i],每个单位时间题目的分数将会减少pointsPerMinute[i],即如果在时间t解决了第 ...

  4. 应用数据存储到sdcard上一定要规范,android4.4.2有新规范

    如果你的android设备有内部存储空间,即通常所说的机身存储(这就是指主要外部存储),那么你从外部插入SD卡就是一个二级外部存储设备. 最新的Android 4.4系统中,外置存储卡(SD卡)被称为 ...

  5. Linux下的JDK安装rpm命令详解

    1. 安装程序 #rpm -ivh jdk-7u79-linux-x64.rpm 出现安装协议等,按接受即可. 2.设置环境变量. #vi /etc/profile JAVA_HOME=/usr/ja ...

  6. POJ 3660 Cow Contest (floyd求联通关系)

    Cow Contest 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/H Description N (1 ≤ N ≤ 100) ...

  7. HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)

    Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...

  8. [iOS UI进阶 - 2.4] 彩票Demo v1.4 转盘动画

    A.需求 幸运广场界面中有一个幸运转盘,平时能够自动缓缓转动 能够选择星座 点击“开始选号”开速旋转转盘,旋转一定周数 转盘转动速度节奏:开始-慢-块-慢-结束 设置其余的背景和按钮   code s ...

  9. Local host name unknown: java.net.UnknownHostException:

    在Linux下安装完resin后,每次启动都出现如下错误: [11:06:45.617] {watchdog-} WatchdogProcess[Watchdog[],7] starting Resi ...

  10. UVa 1252 Twenty Questions (状压DP+记忆化搜索)

    题意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同), 最小需要多少次询问? 析:我们假设心中想的那个物 ...