I built up the same test set, and executed this query on it:
```dataview
TABLE item, i, j
WHERE item
SORT file.ctime
FLATTEN item as i
FLATTEN list(split(i, "\s*-\s*")) as j
```
Which gave me this output:
Here we can see that the initial list of items is shown in second column and after that has been flattened into i
, we see a single item in the third column (with the corresponding list repeated in the second column). This is further split into a list j
which holds the distinct entries (in the fourth column). I’ve used a slightly more accepting regex to accommodate for any amount of spaces around the dash to do the split.
Finding our bits and pieces
Now we’re ready to tackle the main issue, with the knowledge that j[0]
is our item (or thingy
, as I’ve called it in the query below), and the cost
from number(j[1])
translated into a number to make the calculations easier.
Your first question then simply becomes a GROUP BY thingy
, which will move all other fields into the rows
object, so now we can do sum(rows.cost) as sum
to get the sum of costs for that particular item.
You also wanted to present the output with two decimals, which is actually harder than it looks within dataview. You can round the answer to two decimals using round(sum, 2) as sum2
, but when it’s presented and the last digit is zero it’s dropped.
The most reliable way to get two decimals that I’ve found is to multiply by 100, convert to a string, and “manually” insert the decimal point at the correct position. Aka regexreplace(string(round(sum*100)), "(..)$", ".$1") as sum3
.
Here is a query showing all those sums, and doing these operations:
```dataview
TABLE WITHOUT ID thingy, sum, sum2, sum3
WHERE item
SORT file.ctime
FLATTEN item as i
FLATTEN list(split(i, "\s*-\s*")) as j
FLATTEN j[0] as thingy
FLATTEN number(j[1]) as cost
GROUP BY thingy
FLATTEN sum(rows.cost) as sum
FLATTEN round(sum, 2) as sum2
FLATTEN regexreplace(string(round(sum*100)), "(..)$", ".$1") as sum3
```
Which displays as:
This is the verbose version of this query, and you most likely don’t care about all those intermediate sums and so on, so the most condensed version, but still keeping the naming of the cost:
```dataview
TABLE WITHOUT ID key as Item, sum as "Cost"
WHERE item
SORT file.ctime
FLATTEN item as i
FLATTEN list(split(i, "\s*-\s*")) as j
FLATTEN number(j[1]) as cost
GROUP BY j[0]
FLATTEN regexreplace(string(round(sum(rows.cost)*100)), "(..)$", ".$1") as sum
```
Which then displays as: