Higher level Sum() method
Could we get Go methods for SUM, so that we don't have to write as many SQL queries?
I tried passing db.Raw("SUM(net_cost)") to pool.Collection("charge").Select(), but this unfortunately returns zero every time.
I also tried passing db.Func("SUM", "net_cost") to pool.Collection("charge").Select(), but that also returns zero.
For comparison, when I manually type SELECT SUM(net_cost) FROM charge; into mysql CLI, I get the right answer, a non-zero sum.
Update:
I was finally able to get accurate results for this query through upper.io, by making sure to qualify the sum expression as a SQL attribute name, and then decode the result row into an appropriate map / struct.
type sumRecord struct {
Sum float64 `db:"SUM(net_cost)"`
}
result := pool.Collection("charge").Find(constraints).Select(db.Raw("SUM(net_cost)"))
So it looks like the SQL deserialization was dropping the SUM(net_cost) attribute on the floor without so much as a warning log, and my old struct was not validating that its db:"sum" field was actually present in the row.
From this working snippet, I think we can improve upper simply by:
- Validating SQL row deserialization more fully
- Adding convenience methods for querying field sums
With these changes in place, upper.io users would get a lot more reliable code out of the box!