Usage aggregates

Introduction


🚧

Prerequisites

You must configure a data source or a raw metric before trying to create usage aggregates.

Example raw-metric schema

This example-based guide uses a raw metric named telephone usage and the following schema:

{
  "data": {
    "sms": "int",
    "data": "float",
    "call_minutes": "float"
  },
  "timestamp": "timestamp",
  "customer_id": "string"
}

Example raw metric data

The following example events will be used in the guide.

data.smsdata.datadata.call_minutestimestampcustomer_id
433.756.02024-04-16 11:33:38.0008578d067-b019-471c-b28c-5a3f35a3d05a
122.023.02024-04-17 11:25:02.0008578d067-b019-471c-b28c-5a3f35a3d05a
161.834.02024-04-18 11:25:43.0008578d067-b019-471c-b28c-5a3f35a3d05a

Example customer

The customer ID 8578d067-b019-471c-b28c-5a3f35a3d05a belongs to a fictional customer named John Doe.


Add new aggregate

  1. Navigate to Metering > Usage Aggregates in the left side panel.
  2. On the Usage Aggregates page, click on the + ADD NEW AGGREGATE button.
  1. On the Add New Aggregate page, there are two ways to add a new aggregate:
    • Visual builder: a wizard that helps you build an aggregate query
    • SQL builder: a SQL-based tool that helps you handcraft an aggregate query
  1. Create the desired aggregate query using either the visual builder or the SQL builder.
  2. Click on the CREATE AGGREGATE button.

Add an aggregate using the visual builder

The wizard-based visual builder has four main steps:

  1. Select tables and filters
  2. Map customer
  3. Set date field
  4. Set aggregate over

Step 1: select tables and filters

There are two ways you can let Zenskar access your customers' data:

  • data source
  • raw metrics

Select data source

  • Select the table that contains usage data.
  • If the table you selected is related to a data source, the Data Source tag will appear below the Table Name drop-down.

Select raw metrics

  • Select the table that contains usage data.
  • If the table you selected is related to a raw metric, the Raw Metric tag will appear below the Table Name drop-down.

In this guide, we will use the telephone usage raw metric.

Select filters

The raw-metric schema of telephone usage results in the following columns when Zenskar creates a SQL table:

Columns
customer_id
timestamp
data.sms
data.call_minutes
data.data

Filters can be created using any of these columns. The conditions available are dependent on the data type of the column selected. The data type--condition mapping is shown below:

Data typeConditions
int
float
bigint
double
tinyint
smallint
is
is not
less than
greater than
is empty
is not empty
booleanis
is not
is empty
is not empty
date
timestamp
is
is not
is before
is after
is empty
is not empty
stringis
is not
contains
does not contain
starts with
ends with
is empty
is not empty

A simple filter

For the example data mentioned above, a simple filter based on timestamp can be created:

ColumnConditionValue
timestampis before2024-04-18

This filter will yield the following result:

customer_idtimestampdata.call_minutesdata.smsdata.data
8578d067-b019-471c-b28c-5a3f35a3d05a2024-04-17 11:25:02.00023.0122.0
8578d067-b019-471c-b28c-5a3f35a3d05a2024-04-16 11:33:38.00056.0433.7

All the raw-metric data that you sent to Zenskar for the cusotmer 8578d067-b019-471c-b28c-5a3f35a3d05a that is before the date 2024-04-18 is returned as the result..

A complex AND filter

For the example data mentioned above, a complex filter based on timestamp AND data.call_minutes can be created:

ColumnConditionValue
timestampis before2024-04-18
data.call_minutesgreater than30

This filter will yield the following result:

customer_idtimestampdata.call_minutesdata.smsdata.data
8578d067-b019-471c-b28c-5a3f35a3d05a2024-04-16 11:33:38.00056.0433.7

All the raw-metric data that you sent to Zenskar for the cusotmer 8578d067-b019-471c-b28c-5a3f35a3d05a before the date 2024-04-18 ANDwhere data.call_minutes is greater than 30 is returned as the result.

A complex OR filter

Simply clicking on the AND condition will change it to OR and vice versa, as shown below.

For the example data mentioned above, a complex filter based on timestamp OR data.call_minutes can be created:

ColumnConditionValue
timestampis before2024-04-18
data.call_minutesgreater than30

This filter will yield the following result:

customer_idtimestampdata.call_minutesdata.smsdata.data
8578d067-b019-471c-b28c-5a3f35a3d05a2024-04-17 11:25:02.00023.0122.0
8578d067-b019-471c-b28c-5a3f35a3d05a2024-04-18 11:25:43.00034.0161.8
8578d067-b019-471c-b28c-5a3f35a3d05a2024-04-16 11:33:38.00056.0433.7

All the raw-metric data that you sent to Zenskar for the cusotmer 8578d067-b019-471c-b28c-5a3f35a3d05a before the date 2024-04-18 ORwhere data.call_minutes is greater than 30 is returned as the result.

Step 2: map customer

You must select a column in your table that can uniquely identify your customers.

  • This can also be your customer's email ID

  • You must map this column to one of the following columns in Zenskar:

    • email
    • external_id: this is usually a unique identifier (UUID) that your system generates or is generated by your CRM.
    • customer name

🚧

Note

The Zenskar columns email, external_id, and customer name are created when you add your customer to Zenskar.


Step 3: set date field

Step 4: set aggregate over

🚧

Note

Refer the documentation on data processing to understand how aggregates work.

  1. Select the aggregation method:
AggregateDescription
COUNTreturns the number of items found in a column
SUMreturns the sum of all the values, or only the DISTINCT values, in the column. SUM can be used with numeric columns only. Null values are ignored.
MAXreturns the maximum value in a column.
MINreturns the minimum value in a column.
AVGreturns the average of the values in a column ignoring null values.
UNIQUE COUNT
  1. Select the column over which the aggregation must be performed.

COUNT

For the example data mentioned above, COUNT will return 3 for any column.

SUM

For the example data mentioned above, SUM will return the following values:

Aggregate typeAggregate value returned
data.call_minutes113.0
data.sms71
data.data7.5

❗️

NOTE

The SUM aggregate must be performed over columns of numeral data type: int, float, bigint, double, tinyint, smallint. Trying to SUM column of any other data type will result in the following error:

MAX

For the example data mentioned above, MAX will return the following values:

Aggregate typeAggregate value returned
data.call_minutes56.0
data.sms43
data.data3.7

❗️

NOTE

The MAX aggregate must be performed over columns of numeral data type: int, float, bigint, double, tinyint, smallint. Trying to run MAX aggregate over a column of any other data type will result in the following error:

MIN

For the example data mentioned above, MIN will return the following values:

Aggregate typeAggregate value returned
data.call_minutes23.0
data.sms12
data.data1.8

❗️

NOTE

The MIN aggregate must be performed over columns of numeral data type: int, float, bigint, double, tinyint, smallint. Trying to run MIN aggregate over a column of any other data type will result in the following error:

AVG

For the example data mentioned above, AVG will return the following values:

Aggregate typeAggregate value returned
data.call_minutes37.666668
data.sms23.666666666666668
data.data2.5

❗️

NOTE

The AVG aggregate must be performed over columns of numeral data type: int, float, bigint, double, tinyint, smallint. Trying to run AVG aggregate over a column of any other data type will result in the following error:

UNIQUE COUNT

For the example data mentioned above, UNIQUE COUNT will return the following values:

Aggregate typeAggregate value returnedNotes
customer_id1In the example data mentioned above, there exists only one customer with 8578d067-b019-471c-b28c-5a3f35a3d05a as ID.
timestamp3In the example data mentioned above, there exist three unique timestamps.
data.call_minutes3
data.sms3
data.data3

SQL builder

Refer to the SQL-query template documentation to understand how to use the SQL builder.

In the above SQL query, quantity refers to the raw metric being aggregated.