Hwo to use sum function in CBL2


#1

Do you have an example of how to get the sum of an integer property for all documents? I can’t find any examples of how to use the aggregation functions.


#2

You’ll want to post to the right category, or maybe one of the admins can move this (ping @czajkowski @matthew.groves @nraboy)


#3

Is your question regarding cbl 2.x?


#4

Yes it is - (and have to fill out to reach 20 chars for a message :slight_smile:)


#5

I don’t know your platform but here is C#

QueryBuilder.Select(SelectResult.Expression(Function.Sum(Expression.Property("key"))))

This will give you the sum of all of the values of the “key” path in the database.


#6

Ahh, thanks a lot. I’m using Android, but it looks identical. Sorry for the noob question, but how do I then get the value for a function like that?
I would do a query.execute and get a ResultSet. Would that contain a list of Result with one entry, that I can get with result.getInt(“key”) - or what will the name of the key be?


#7

The key in this case will be autogenerated as $1 I think, but it’s easier to just use getInt(0) to get the integer at “index 0” (the first entry in the result).