Distinct count to exclude NULL

久未见 提交于 2019-12-23 16:15:27

问题


Is there a way to create a calculated measure that can do a distinct count, but excluding the null values? For example, if I have: 10, 20, 20, null; the distinct count is 3, but can I have the answer as 2?


回答1:


put a where condition to where the measure is not null




回答2:


you could try this:

with cte ( "counter" ) as
( 
    select 
        1 as "counter" 
    from 
        "YOURTABLE" 
    group by 
        "COLUMNNAME" 
    having ( "COLUMNNAME" IS NOT NULL )
)
select SUM( "counter" ) from cte

@ edit:

quite easier variant:

SELECT COUNT( DISTINCT "COLUMNNAME" ) FROM "TABLENAME"



回答3:


have the same issue - it is amazing microsoft ignores it...looks like the only solution is to build a view for distinct count measure group

http://www.sqlservercentral.com/Forums/Topic810069-17-1.aspx#bm1213461




回答4:


Thank you for all the answers. However, here I found a solution that works best for my need: http://richardlees.blogspot.com/2008/10/alternative-to-physical-distinct-count.html

By creating a Calculated Member using the following:

count(nonempty({[DimName].[HierarchyName].[LevelName].members-[DimName].[HierarchyName][All].UNKNOWNMEMBER},[Measures].[MyMeasure]))



回答5:


simply add where value is not null and value "!=''" that should do the trick



来源:https://stackoverflow.com/questions/13914039/distinct-count-to-exclude-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!