Why NULL values are mapped as 0 in Fact tables?

后端 未结 4 1980
深忆病人
深忆病人 2020-12-21 00:40

What is the reason that in measure fields in fact tables (dimensionally modeled data warehouses) NULL values are usually mapped as 0?

4条回答
  •  误落风尘
    2020-12-21 01:18

    NULL instead of 0 should be used if you intend to do an average on your fact column. This is the only time i believe NULLS are ok in a dwh fact or dimensions

    if a fact value is unknown/late arriving, then leaving as NULL is best.

    aggregate functions suchs as MIN,MAX work on NULLS simply ignoring them

    (For the record one of Ralph Kimball's sidekicks said this in his course I intended)

    with goodf as
    (
    select 1  x
    union all
    select null 
    union all
    select 4
    )
    select sum(x) sumx,min(x) minx,max(x) maxx,avg(cast(x as float)) avgx 
    from goodf
    
    
    with badf as
    (
    select 1  x
    union all
    select 0 /* unknown */ 
    union all
    select 4
    )
    select sum(x) sumx,min(x) minx,max(x) maxx,avg(cast(x as float)) avgx 
    from badf
    

    in badf above the average comes out incorrect as it uses the zero of the unknown value as literally 0

提交回复
热议问题