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

calculate exponential moving average in python

EDIT: It seems that mov_average_expw() function from scikits.timeseries.lib.moving_funcs submodule from SciKits (add-on toolkits that complement SciPy) better suits the wording of your question. To calculate an exponential smoothing of your data with a smoothing factor alpha (it is (1 – alpha) in Wikipedia’s terms): >>> alpha = 0.5 >>> assert 0 < alpha <= 1.0 … Read more

Weighted averaging a list

You could use numpy.average to calculate weighted average. In [13]: import numpy as np In [14]: rate = [14.424, 14.421, 14.417, 14.413, 14.41] In [15]: amount = [3058.0, 8826.0, 56705.0, 30657.0, 12984.0] In [17]: weighted_avg = np.average(rate, weights=amount) In [19]: weighted_avg Out[19]: 14.415602815646439

SQL query with avg and group by

If I understand what you need, try this: SELECT id, pass, AVG(val) AS val_1 FROM data_r1 GROUP BY id, pass; Or, if you want just one row for every id, this: SELECT d1.id, (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 WHERE d2.id = d1.id AND pass = 1) as val_1, (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM … Read more

Calculating the averages for each KEY in a Pairwise (K,V) RDD in Spark with Python

Now a much better way to do this is to use the rdd.aggregateByKey() method. Because this method is so poorly documented in the Apache Spark with Python documentation — and is why I wrote this Q&A — until recently I had been using the above code sequence. But again, it’s less efficient, so avoid doing … Read more

Mysql Average on time column?

Try this: SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(`login`))) FROM Table1; Test data: CREATE TABLE `login` (duration TIME NOT NULL); INSERT INTO `login` (duration) VALUES (’00:00:20′), (’00:01:10′), (’00:20:15′), (’00:06:50′); Result: 00:07:09

tech