Calculate a column total from Dataview table

What I’m trying to do

Hello I looking to use the Dataview plugin to manage my cigar collection, similar to what others have done to keep track of their book library. The idea being that I will create a note with properties for each cigar I own so I can query what is in my cigar collection. Here is the note template I am using for this:

This is the query I am using to track what currently own:

TABLE
	size AS "Size",
	qty AS "Qty",
	dateformat(date_added, "MM-dd-yyyy") AS "Date added",
	durationformat(date(now) - date_added, "d") AS "Days aged"
FROM "Cigars"
WHERE humidor = "yes"
SORT Cigar ASC

The one thing I can’t seem to get to work is creating a query that will calculate the sum total based on the qty column. I have been unable to find a post online with a solution for this problem. I tried getting to ChatGPT to provide a solution, but nothing so far has worked.

Things I have tried

creating a total qty column

TABLE 
	size AS "Size",
	qty AS "Qty",
	dateformat(date_added, "MM-dd-yyyy") AS "Date added",
	durationformat(date(now)-date_added, "d") AS "Days aged",
	sum(qty) AS "Total Qty"
FROM "Cigars"
WHERE humidor = "Yes"
SORT Cigar ASC

This gives the total for each row instead of the sum total across all rows.

creating a list

LIST
sum(qty)
FROM "Cigars"
WHERE humidor = "Yes"

This also gives the total for each row instead of the sum total across all rows.

using inline code

$= sum(from("Cigars").where(humidor = "yes").qty)

This gives the following error, ReferenceError: sum is not defined

Does the following give the sum total across all rows in your vault?

```dataview
TABLE WITHOUT ID 
Quantity
WHERE qty AND typeof(qty) = "number"
GROUP BY ""
FLATTEN sum(rows.qty) as Quantity
```

Yes, this code works!

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.