average

Mysql AVG to ignore zero

社会主义新天地 提交于 2019-12-02 19:15:24
I need to perform an avg on a column, but I know that most of the values in that column will be zero. Out of all possible rows, only two will probably have positive values. How can I tell mySQL to ignore the zeros and only average the actual values? Assuming that you might want to not totally exclude such rows (perhaps they have values in other columns you want to aggregate) SELECT AVG(NULLIF(field ,0)) from table You could probably control that via the WHERE clause: select avg( field ) from table where field > 0 select avg(your_column) from your_table where your_column != 0 Barvajz You can

Generate a random number with max, min and mean(average) in Java

拜拜、爱过 提交于 2019-12-02 19:07:31
I need to generate random numbers with following properties. Min should be 200 Max should be 20000 Average(mean) is 500. Optional: 75th percentile to be 5000 Definitely it is not uniform distribution, nor gaussian. I need to give some left skewness. Java Random probably won't work because it only gives you normal(gaussian) distributions. What you're probably looking for is an f distribution (see below). You can probably use the distlib library here and choose the f distribution . You can use the random method to get your random number. Say X is your target variable, lets normalize the range by

ActiveRecord AVG calculation

≡放荡痞女 提交于 2019-12-02 16:49:52
问题 I have request: Model.group(:p_id).pluck("AVG(desired)") => [0.77666666666666667e1, 0.431666666666666667e2, ...] but when I ran SQL SELECT AVG(desired) AS desired FROM model GROUP BY p_id I got ----------------- | desired | |-----------------| | 7.76666666666667| |43.1666666666667 | | ... | ----------------- What is the reason of this? Sure I can multiply, but I bet where are should be an explanation. I found that Model.group(:p_id).pluck("AVG(desired)").map{|a| a.to_f} => [7.76666666666667

MYSQL PHP getting average of a single field

笑着哭i 提交于 2019-12-02 16:40:19
问题 I've made a form that allows users to submit their name, a comment and a score from 1-6, which then is saved in a table in their respective fields; name, comment and score. I wan't to display the average score. This is what I've found out so far: $result = mysql_query("SELECT AVG(fieldName) FROM tableName"); How do I echo this out? 回答1: Give your result an alias, It makes accessing it easier. Use mysql_fetch_assoc() to get your results $result = mysql_query("SELECT AVG(fieldName) AS avg FROM

Code igniter database calculate column average

随声附和 提交于 2019-12-02 15:03:15
问题 I need to calculate in a function the average score of a column named: "totalscore" from my database table "score" I tried to do Active record select_avg() but I am not getting anything. Any idea how I can do this? function calculateaverage(){ $dataArr = array(); $data = $this->db->get('score'); $maxrows = $data->num_rows(); $data = $this->db->get('score'); for ($i = 1; $i<= $maxrows-1; $i++){ $this->db->select('totalscore'); foreach ($data->result() as $row) { $dataArr[$i] = $row->totalscore

How to find the lowest, highest and average values in a listbox [closed]

ⅰ亾dé卋堺 提交于 2019-12-02 13:34:07
I'm trying to create a program that calculates and displays the highest, lowest and average value of items in a listbox (items generated from a txt file). I finally figured out how to load a text file to the listbox. I have been searching for clues for about an hour and all my attempts have brought me to a dead end. my listbox is called readListbox and my Highest, Lowest and Average labels are called highestLabel, lowestLabel and averageLabel respectively. How do I go about creting this program. The numbers are in the decimal format. Any Help will be very much appreciated. private void

how to select, average and sort in mysql table

狂风中的少年 提交于 2019-12-02 13:06:08
i have a table in mySql like in this picture and i want to write a query which result will group by LESSON column, and add new row which is average value of LESSON column and sum CNT column values.... for this query i use this one i use this query but it gives result like in picture 3 and i cant sort by PERC in this case select no, STUD_ID,CLASS,LESSON, AVG(PERC) as PERC,SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by CLASS union all select no,STUD_ID,CLASS,'AVERAGE' as LESSON, AVG(PERC) as PERC, SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by LESSON It looks like you're

how to avoid the potential of an overflow when computing an average of times?

久未见 提交于 2019-12-02 12:06:35
问题 I am writing a function for getting the average of the clocks it takes to call a specific void (*)(void) aka void -> void function a specific number of times. I am worried that it if the sample size gets too large, the sum of the observations will overflow and make the average invalid. is there a standard approach to removing the possibility of sum overflowing in these kinds of problems? Note: I understand that this example is too naive to conclude anything about performance; I am interested

Average the sum of rows without a creating new column in Excel

走远了吗. 提交于 2019-12-02 11:48:31
问题 Here's a sample of my matrix: A B C D E 1 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 2 1 You can think of each row as a respondent and each column as an item on a questionnaire. My goal is to take an average of the sum of each row (i.e. total score for each respondent) without creating a new column AND accounting for the fact that some or all of the entries in a given row are empty (e.g., some respondents missed some items [see row 5] or didn't complete the questionnaire entirely [see row 3]). The desired

Average and Case in SQL

杀马特。学长 韩版系。学妹 提交于 2019-12-02 10:35:57
I am trying to generate a report and the following code does not produce the desired results which gives me 2 lines rather than one. The ScoreTypeID could have values of 22, 52, 3 or 4 . if it is 22 or 52, I need the average and if not I need to show 0. Any idea what may be the problem ? Thanks. CASE WHEN FAS1.ScoreTypeID = 22 THEN avg(fas1.totalscore) WHEN FAS1.ScoreTypeID = 52 THEN avg(fas1.totalscore) ELSE 0 END AS 'Total Score', I think in your full query, you are missing the GROUP BY clause, eg SELECT ... FROM .... WHERE .. GROUP BY FAS1.ScoreTypeID I think this is what you want: coalesce