sum
Why SUM(`column`) returns a string instead of an integer?
Answer recommended by PHP Collective
Avg of a Sum in one query
I think your question needs a bit of explanation. If you want to take the sums grouped by t.client you can use: SELECT t.client, SUM(t.asset) FROM the-table t GROUP BY t.client Then, if you want to take the average of this sume, just make: SELECT AVG(asset_sums) FROM ( SELECT t.client, SUM(t.asset) AS asset_sums FROM the-table … Read more
Efficient summation in Python
Here’s a very fast way: result = ((((12 * n + 45) * n + 50) * n + 15) * n – 2) * n // 120 How I got there: Rewrite the inner sum as the well-known x*(x+1)//2. So the whole thing becomes sum(x**2 * x*(x+1)//2 for x in range(n+1)). Rewrite to sum(x**4 … Read more
How to avoid floating point precision errors with floats or doubles in Java? [duplicate]
There is a no exact representation of 0.1 as a float or double. Because of this representation error the results are slightly different from what you expected. A couple of approaches you can use: When using the double type, only display as many digits as you need. When checking for equality allow for a small … Read more
Using SUM() without grouping the results
SELECT a.id, b.amount FROM table1 a CROSS JOIN ( SELECT SUM(amount) amount FROM table1 ) b You need to perform a cartesian join of the value of the sum of every row in the table to each id. Since there is only one result of the subselect (49), it basically just gets tacked onto each … Read more
Percentage from Total SUM after GROUP BY SQL Server
You don’t need a cross join. Just use window functions: SELECT P.PersonID, SUM(PA.Total), SUM(PA.Total) * 100.0 / SUM(SUM(PA.Total)) OVER () AS Percentage FROM Person P JOIN Package PA ON P.PersonID = PA.PackageFK GROUP BY P.PersonID; Note that you do not need the JOIN for this query: SELECT PA.PersonID, SUM(PA.Total), SUM(PA.Total) * 100.0 / SUM(SUM(PA.Total)) OVER … Read more
How do I sum the values in an array of maps in jq?
Unless your jq has inputs, you will have to slurp the objects up using the -s flag. Then you’ll have to do a fair amount of manipulation: Each of the objects needs to be mapped out to key/value pairs Flatten the pairs to a single array Group up the pairs by key Map out each … Read more