Is it possible to sum the contents field within a group

I have a table with 3 columns
Id, catagory and time in minutes

I have a repeat with a group on the catagory,
and this within the repeat Catagory: {{$key}} Number of Entries: {{$value.count()}}
is it possible to also output a sum for the time in minutes on the same line?

So for instance it would output the following (if all three time in minutes entries where 15 mins each)
Catagory : Walking Number of entries:3 Time in minutes: 45
Catagory : Running Number of entries:2 Time in minutes: 30

If you’re looking at 15 minutes per event, you should be able to use:

Time in minutes: {{$value.count()*15}}

Thanks for the response bpj
unfortunatly the time in minutes field can be anything from 15mins upto 1440mins
Sorry I didnt make myself clear
so I need to count the number of entries and also sum time in minutes

Ah ok, you shoul dbe able to do something like this then:

serverconnectid.data.query.where('category',category,'==').sum('time in minutes')

Assuming the time in minutes value is the same for all entries for a given category, you can then add the $value.count()* as per earlier:
i.e.

$value.count()*serverconnectid.data.query.where('category',category,'==').sum('time in minutes')

Thanks again for your help i am still just learning

Im not sure if that will work as the time in minutes value is not always the same for a given category
The table would look something like this
id category time in minutes
1 Walking 15
2 Walking 30
3 Walking 45
4 Running 15
5 running 45

I need it to output in the repeat
Category : Walking Number of entries:3 Time in minutes: 90
Category : Running Number of entries:2 Time in minutes: 60

The last art of my previous post was slightly wrong. You don’t need the $value.count* as it is already doing a sum of the time in minutes. Your whole line would be like this:

Catagory: {{$key}} Number of Entries: {{$value.count()}} Time in minutes: {{serverconnectid.data.query.where('catagory',$key,'==').sum('time in minutes')}}

I’m not sure if it’ll work but you could also try

$value.sum('time in minutes')

(assuming the time column is called ‘time in minutes’ - adjust as necessary)

Catagory: {{$key}} Number of Entries: {{$value.count()}} Time in minutes: {{$value.sum('time in minutes')}}

This worked (I had tried this previously and for some reason it didn’t work, tried it again as per your suggestion and it works perfectly)
Thanks so much for all your help!!!

1 Like