Search code examples
sqlnaming-conventionsdata-warehousedimensional-modeling

Should an aggregate data item have the same name as a detail data item?


Consider the following SQL:

   select dimension, sum(fact) as fact
   from t
   group by dimension;

Here, I have given the aggregated fact the same name as the granular, un-aggregated fact. Is this considered a good or a bad idea?

Pro: the aggregated data item is, in all ways save its dimension, the "same type of" data item as the detailed data item.

Con: Because of the dimensional difference, it is nevertheless not the same type of data item, and care should be taken when combining it with other data items - therefore it is best to distinguish it by calling it something like fact_agg.


Solution

  • I always prefix with the aggregation name, so in your case SumFact. This is important if you have multiple aggregations on the same fact, such as:

    Select Dimension, 
           COUNT(Fact) as CountFact, 
           SUM(Fact) as SumFact, 
           AVG(Fact) as AvgFact
    from t
    group by Dimension