Skip Navigation
sum() vs count() functions
Answer ID 4417   |   Last Review Date 12/18/2018

When do I want to use the sum() function and when do I want to use count() function?


Analytics, Functions


The count() aggregate function is used for counting the number of items (think number of items). The sum() aggregate function is used for summing up items (think mathematical addition).

For example, we have two incidents who's i_id's are 100, and 101.

COUNT: The result of the expression count(incidents.i_id) be 2.

SUM:  If we use sum(incidents.i_id), the result would be 201, as it would sum the numbers 100 + 101.

There may be times when you want to know how many incidents a contact has. In those cases you would want to use the count() function.

One example where sum() would come in handy would be with the stats table.

The stats table collects data on an hourly basis so you may have data for sessions of:
 stat_date  sessions
 12:00  500
 13:00  600
 14:00  500

You would not want to use count() to get your total sessions because you would end up with 3, as there are 3 records in our example. In this case you would want to use sum(stats.sessions).  The sum of the sessions for three hours would be 1600 (500 + 600 + 500).