average

Creating calculated fields in sql

ぐ巨炮叔叔 提交于 2019-12-05 21:52:15
This will seem rudimentary but I can't find a concise example online that matches up. I have three fields; m1 , m2 , and m3 . I need to create a column or field that is the average of them three. The calculated field would be titled employment. Would the following code be suffice? ALTER TABLE dbo.tablename ADD Employment AS Select ((m1+m2+m3)/3) Sample data m1 20 20 30 m2 15 17 25 m3 60 77 13 desired result. Name m1 m2 m3 Employment Auto body 20 20 30 23 Auto Parts 15 17 25 19 Auto Sales 60 77 13 50 You are very close, it's called Computed Column https://technet.microsoft.com/en-us/library

Moving average - MySQL

好久不见. 提交于 2019-12-05 21:39:21
I'm trying to implement system-wide login throttling and I need to calculate the daily average number of failed login attempts from the last 3 months. I'm currently inserting a record on every login fail, each with a timestamp. How can I do this in MySQL? Thanks in advance for your help SELECT AVG(cnt) FROM (SELECT COUNT(*) AS cnt FROM mytable WHERE `date` BETWEEN DATE_SUB(NOW(), INTERVAL 3 MONTH) AND NOW() GROUP BY DATE(`date`)) x Assuming you have a table mytable with field date of type date , datetime or timestamp 来源: https://stackoverflow.com/questions/5036176/moving-average-mysql

SQL Work out the average time difference between total rows

我的梦境 提交于 2019-12-05 19:28:56
I've searched around SO and can't seem to find a question with an answer that works fine for me. I have a table with almost 2 million rows in, and each row has a MySQL Date formatted field. I'd like to work out (in seconds) how often a row was inserted, so work out the average difference between the dates of all the rows with a SQL query. Any ideas? -- EDIT -- Here's what my table looks like id, name, date (datetime), age, gender If you want to know how often (on average) a row was inserted, I don't think you need to calculate all the differences. You only need to sum up the differences

prevent long running averaging from overflow?

六月ゝ 毕业季﹏ 提交于 2019-12-05 17:43:56
suppose I want to calculate average value of a data-set such as class Averager { float total; size_t count; float addData (float value) { this->total += value; return this->total / ++this->count; } } sooner or later the total or count value will overflow, so I make it doesn't remember the total value by : class Averager { float currentAverage; size_t count; float addData (float value) { this->currentAverage = (this->currentAverage*count + value) / ++count; return this->currentAverage; } } it seems they will overflow longer, but the multiplication between average and count lead to overflow

Take the average of two signed numbers in C

风格不统一 提交于 2019-12-05 15:28:42
问题 Let us say we have x and y and both are signed integers in C, how do we find the most accurate mean value between the two? I would prefer a solution that does not take advantage of any machine/compiler/toolchain specific workings. The best I have come up with is: (a / 2) + (b / 2) + !!(a % 2) * !!(b %2) Is there a solution that is more accurate? Faster? Simpler? What if we know if one is larger than the other a priori? Thanks. D Editor's Note : Please note that the OP expects answers that are

Python, Pandas: average every 2 rows together

Deadly 提交于 2019-12-05 10:37:49
pretty basic question, but was wondering: What is the 'proper' way to average every 2 rows together in pandas Dataframe, and thus end up with only half the number of rows? Note that this is different than the rolling_mean since it reduces the number of entries. A fast way to do it: >>> s = pd.Series(range(10)) >>> s 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 >>> ((s + s.shift(-1)) / 2)[::2] 0 0.5 2 2.5 4 4.5 6 6.5 8 8.5 The "proper way" I guess would be something like: >> a = s.index.values >>> idx = np.array([a, a]).T.flatten()[:len(a)] >>> idx [0 0 1 1 2 2 3 3 4 4] >>> s.groupby(idx).mean() 0 0

SQL Query to find average of difference between dates

本小妞迷上赌 提交于 2019-12-05 09:29:43
问题 I have a column that has data like Date 13/8/2011 2/9/2011 10/9/2011 20/9/2011 I need to write a SQL query/procedure that will help me get the average of the differences between the dates. For the above example it would be (19+8+10)/3=12.33 Please help with this. Thanks in Advance, Geetha 回答1: I don't know your RDBMS, but this is from SQL Server. Also, one of your calcs is wrong - 02/09/2011 - 13/08/2011 is 20, not 19. create table dates ( myDate date ) insert into dates values ({d '2011-08

calculate the average of three encrypted numbers

与世无争的帅哥 提交于 2019-12-05 08:20:54
Is possible to calculate average of three encrypted integer? No constrain on the method of encrypting. The point of this is just to hide the three numbers and find average. What you seem to be looking for is called Homomorphic Encryption : an encryption scheme which allows you to perform operations on encrypted data, with the encrypted result as the outcome. Such a scheme would allow you to give encrypted data to a 3rd party, which could then do computations on it for you without knowing what they were computing. In your case, you need two operations: addition and division. Until recently,

sql - find average salary for each department with more than five members

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 07:44:11
问题 Not quite sure how to get this one. I have a staff table and I need to find the average salary. I know I can use use avg() . But the trick is I need to find the average for departments that have more than 5 staff members. I'm not sure if I should use group by or how to use it. Thanks! CREATE TABLE STAFF (STAFF_ID CHAR(3), STAFF_NAME CHAR(20), GENDER CHAR(6), DEPARTMENT CHAR(20), BOSS_ID CHAR(3) SALARY NUMBER(8,2)); 回答1: select DEPARTMENT,count(STAFF_ID) as CountStaff, avg(SALARY) as AVGSalary

What's the quickest way to get the mean of a set of numbers from the command line?

和自甴很熟 提交于 2019-12-05 01:37:25
Using any tools which you would expect to find on a nix system (in fact, if you want, msdos is also fine too), what is the easiest/fastest way to calculate the mean of a set of numbers, assuming you have them one per line in a stream or file? Awk awk '{total += $1; count++ } END {print total/count}' awk ' { n += $1 }; END { print n / NR }' This accumulates the sum in n , then divides by the number of items ( NR = Number of Records). Works for integers or reals. Using Num-Utils for UNIX: average 1 2 3 4 5 6 7 8 9 perl -e 'while (<>) { $sum += $_; $count++ } print $sum / $count, "\n"'; In