In my previous post, Fun With KQL – Max, MaxIf, Min and MinIf, we looked at the aggregation functions
min. In this post we’ll talk about another aggregation function,
sum. We’ll also look at a variant of it,
I should mention the samples in this post will be run inside the LogAnalytics demo site found at https://aka.ms/LADemo. This demo site has been provided by Microsoft and can be used to learn the Kusto Query Language at no cost to you.
If you’ve not read my introductory post in this series, I’d advise you to do so now. It describes the user interface in detail. You’ll find it at https://arcanecode.com/2022/04/11/fun-with-kql-the-kusto-query-language/.
Note that my output may not look exactly like yours when you run the sample queries for several reasons. First, Microsoft only keeps a few days of demo data, which are constantly updated, so the dates and sample data won’t match the screen shots.
Second, I’ll be using the column tool (discussed in the introductory post) to limit the output to just the columns needed to demonstrate the query. Finally, Microsoft may make changes to both the user interface and the data structures between the time I write this and when you read it.
sum function is straightforward. It creates a total for the column passed into it.
In this demo we’ll take the Perf table, and filter it with a
where operator to only include rows where the CounterName is Free Megabytes.
We then call
summarize, which is needed if you want to use an aggregation function on its own. Finally we use
sum and pass in the column we want to total, CounterValue.
In this example, we result in a total of 6,398,287,032.
Including the Sum As A Column
We can include the
sum function as a column in the output. To do so we need to include a
by as part of the summarize so the data will be grouped correctly, in this case by the CounterName.
In the output you can see each CounterName on a row, along with the grand total for its CounterValue.
sumif uses two parameters. The first is the column to aggregate. The second is the condition which, if true, causes a value to be included in the summation process.
In the example below, the CounterValue will only be included if the CounterName equals Free Megabytes.
Other Uses for SumIf
We can use
sumif just as we did
minif. In fact, lets extend an example from the previous blog post on Max, MaxIf, Min and MinIf and add
sumif to our output.
Here we added two new columns with our summed values.
You can use
sumif like you do
minif. Refer back to the Fun With KQL – Max and Min for other examples.
The following operators, functions, and/or plugins were used or mentioned in this article’s demos. You can learn more about them in some of my previous posts, linked below.
We learned about
sumif in this post, seeing how they can be used. They can be used to return a single grand total, as well as be included as a column in the output of queries.
The demos in this series of blog posts were inspired by my Pluralsight courses on the Kusto Query Language, part of their Kusto Learning Path.
The first course in the series is Kusto Query Language: Getting Started. The second course is Kusto Query Language: Beginning Operators. The third course, to be published soon, is Kusto Query Language: Basic Scalar Operators
I have two previous Kusto courses on Pluralsight as well. Kusto Query Language (KQL) from Scratch and Introduction to the Azure Data Migration Service, and these are two of the many courses I have on Pluralsight. All of my courses are linked on my About Me page.
If you don’t have a Pluralsight subscription, just go to my list of courses on Pluralsight . At the top is a Try For Free button you can use to get a free 10 day subscription to Pluralsight, with which you can watch my courses, or any other course on the site.