Question
There is a table. It has 2 columns ā one is Type, the other is Number. In the Type column, there are repeating values. That is, each Type has several rows.
We need to get a rowList which also has two columns, but the values in the Type column are now unique, and in the Number column, everything is summed up by Type.
Answer
There are many ways, one of them is to take rowList and perform actions on it:
Take the column with Type and make it unique
Create a list of unique rowList
Iterate over this new rowList
filtering rows from the first rowList
and summing the values
=: listReplace(list: $rowlistcreate; action: "sum" = $sum; value: "value")
rowlistcreate: rowListCreate(field: "type" = $uniq)
uniq: listUniq(list: $rowlist[[type]])
~rowlist: selectRowList(table: $#ntn; field: 'type'; field: 'sum')
sum: listSum(list: $filter[[sum]])
filter: listFilter(list: $rowlist; key: "type" = $#value[type])
We get:
[
{
"type": 1,
"sum": 40
},
{
"type": 2,
"sum": 35
},
{
"type": 3,
"sum": 13
}
]