473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Interrows average value?

Hello, I have a table which is:

Expand|Select|Wrap|Line Numbers
  1. DROP TABLE IF EXISTS dummy;
  2. CREATE TABLE dummy (
  3.   id int(11) not null auto_increment,
  4.   entered int(11) not null default 0,
  5. primary key(id)
  6. );
And dummy records for you to easily test:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO dummy (entered) VALUES
  2. (100), (150), (200), (250), (350);
OK !

I need to find out the average between row.entereds. In this example,

150-100 = 50
200-150 = 50
250-200 = 50
350-250 = 100

(50+50+50+100)/4 = 62.5

What a simple query do I need to find that 62.5 value?


Thanks in advance.
May 24 '07 #1
10 2157
chandu031
78 Expert
Hello, I have a table which is:

Expand|Select|Wrap|Line Numbers
  1. DROP TABLE IF EXISTS dummy;
  2. CREATE TABLE dummy (
  3.   id int(11) not null auto_increment,
  4.   entered int(11) not null default 0,
  5. primary key(id)
  6. );
And dummy records for you to easily test:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO dummy (entered) VALUES
  2. (100), (150), (200), (250), (350);
OK !

I need to find out the average between row.entereds. In this example,

150-100 = 50
200-150 = 50
250-200 = 50
350-250 = 100

(50+50+50+100)/4 = 62.5

What a simple query do I need to find that 62.5 value?


Thanks in advance.
Hi blackflicker,

The following code will give you the result:

Expand|Select|Wrap|Line Numbers
  1.  
  2. select avg(b.entered - a.entered)  from (select id r1,entered from dummy) a , 
  3. (select id r2 , entered from dummy )b where  r2 = r1 + 1 
  4.  
  5.  
I think you will have to use a different logic than just r2 = r1 + 1 as there might be no continuity in the Identity values.
I am sure you will be able to figure that logic out.

Cheers!!
May 25 '07 #2
pradeep kaltari
102 Expert 100+
Hello, I have a table which is:

Expand|Select|Wrap|Line Numbers
  1. DROP TABLE IF EXISTS dummy;
  2. CREATE TABLE dummy (
  3.   id int(11) not null auto_increment,
  4.   entered int(11) not null default 0,
  5. primary key(id)
  6. );
And dummy records for you to easily test:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO dummy (entered) VALUES
  2. (100), (150), (200), (250), (350);
OK !

I need to find out the average between row.entereds. In this example,

150-100 = 50
200-150 = 50
250-200 = 50
350-250 = 100

(50+50+50+100)/4 = 62.5

What a simple query do I need to find that 62.5 value?


Thanks in advance.
Hi Blackflicker,
The following query gives the solution you require:
Expand|Select|Wrap|Line Numbers
  1. SELECT AVG(a.entered-b.entered) FROM (SELECT * FROM dummy) a, (SELECT * FROM dummy ) b
  2. WHERE a.id= ( SELECT MIN(id) FROM dummy WHERE id>b.id)
  3.  
If you delete some rows then there will not be continuity in the id's. Hence, You need the SELECT statement in the where clause.
But I am not sure how efficient this query is. I hope this helps.

Regards,
Pradeep
May 25 '07 #3
Motoma
3,237 Expert 2GB
How about:
Expand|Select|Wrap|Line Numbers
  1. SELECT (max(entered) + min(entered)) / count(*) FROM dummy GROUP BY entered
  2.  
You see, all of the interior values (everything that isn't the largest value or the smallest value) get canceled out, because they are both added and subtracted. In the end you just have the difference of the max and min averaged by the total number of entries.
May 25 '07 #4
pradeep kaltari
102 Expert 100+
How about:
Expand|Select|Wrap|Line Numbers
  1. SELECT (max(entered) + min(entered)) / count(*) FROM dummy GROUP BY entered
  2.  
You see, all of the interior values (everything that isn't the largest value or the smallest value) get canceled out, because they are both added and subtracted. In the end you just have the difference of the max and min averaged by the total number of entries.
Hi Motoma,
I guess the expected answer is 62.5 as posted by the the query postee.
I dont think your query will lead to expected results. Could you please explain your query and what were you trying to say?

Regards,
Pradeep.
May 25 '07 #5
Motoma
3,237 Expert 2GB
Hi Motoma,
I guess the expected answer is 62.5 as posted by the the query postee.
I dont think your query will lead to expected results. Could you please explain your query and what were you trying to say?

Regards,
Pradeep.
MY APOLOGIES!! It is still early here, I fired that off without testing it :P

The correct code would be:
Expand|Select|Wrap|Line Numbers
  1. SELECT (max(entered)-min(entered))/(count(*)-1) FROM dummy
  2.  
I forgot that there were five rows.
The logic is still the same.
May 25 '07 #6
Motoma
3,237 Expert 2GB
MY APOLOGIES!! It is still early here, I fired that off without testing it :P

The correct code would be:
Expand|Select|Wrap|Line Numbers
  1. SELECT (max(entered)-min(entered))/(count(*)-1) FROM dummy
  2.  
I forgot that there were five rows.
The logic is still the same.
Reexamining the problem, this solution will only work in cases where the rows are increasing in value.
May 25 '07 #7
pradeep kaltari
102 Expert 100+
MY APOLOGIES!! It is still early here, I fired that off without testing it :P

The correct code would be:
Expand|Select|Wrap|Line Numbers
  1. SELECT (max(entered)-min(entered))/(count(*)-1) FROM dummy
  2.  
I forgot that there were five rows.
The logic is still the same.
Hi Motoma,
That was an eye opener. Good one.
(Why I didn't think in that way, initially? Anyways, I am getting lots to learn here. Thanks)

Regards,
Pradeep.
May 25 '07 #8
If you delete some rows then there will not be continuity in the id's. Hence, You need the SELECT statement in the where clause.
But I am not sure how efficient this query is. I hope this helps.

Regards,
Pradeep
Hello Pradeep, thanks for your time and the answer. Although not an efficiently query as you have explained, at least I have got some clues how to find it.

Actually, this is a dummy table but the real table has a lot of columns that should be displayed in the same result set that I would use from PHP, but again I have got the clues because of you and chandu031. Thanks friends.
May 26 '07 #9
Reexamining the problem, this solution will only work in cases where the rows are increasing in value.
Hi Motoma,

Thanks for your time. Your query is really really simple given respectful clues to me how to do the work. Besides, my real table includes a time column which would be sorted before getting an average for row continuity. Actually, the row pattern going to stay as SUM(r2-r1)/COUNT(*)-1, but the rows going to be sorted for the unix epoch time column.
May 26 '07 #10
Motoma
3,237 Expert 2GB
Hi Motoma,

Thanks for your time. Your query is really really simple given respectful clues to me how to do the work. Besides, my real table includes a time column which would be sorted before getting an average for row continuity. Actually, the row pattern going to stay as SUM(r2-r1)/COUNT(*)-1, but the rows going to be sorted for the unix epoch time column.
Okay, just remember the formula. After sorting (regardless of the values used) the average difference will be always be:

((Value of the last row) - (Value of the first row)) / ((Number of rows) - 1)
May 26 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: StraightEight | last post by:
Hi, I'm am looking for a little help. I need to create a SQL view which joins a few tables, and I need to return an average for a particular column where a second duplicate ID exists... Heres...
3
by: CSDunn | last post by:
Hello, I have 14 fields on a report that hold integer values. The field names use the following naming convention: T1Number, T2Number ....T14Number. I need to get a 'sub total' of all fields as...
3
by: Tony Lennard | last post by:
I have several queries, which generate about 10 fields each a text field of length 2 (which contain effort and attainment grades), eg A2, B4, A3, A3. I am trying to calculate an 11th fields, which...
6
by: J | last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller program. Heres's what is supposed to happen: Roll 2 6-sided dies. Add rolls together put total in rolls(d6total). Display...
5
by: Carlo B | last post by:
I need to store numbers in an array and by using a class I need to calculate the average of the numbers entered. I cannot get the text box to return the total of the numbers in the average function...
3
by: C++Geek | last post by:
I need to get this program to average the salaries. What am I doing wrong? //Program to read in employee data and calculate the average salaries of the emplyees.
1
by: Theadmin77 | last post by:
Well ...this is a real challenge .....i got everything else working OK...but ... I have to get the average and maximum value out of a group of people thru two functions .I have problems passing...
1
by: sadhana123 | last post by:
i have matrix of large data say 1700 row and 500 in coloumn in this there entry is given as rate value from 1- 5 . some values are missing . so i have to find out a predict value for each col value...
4
by: =?Utf-8?B?YjF1Y2VyZWU=?= | last post by:
Hi, still very new to programming but am a little stumped by how to do this idea i have. I need to make a moving average which takes every nth value in a data series to build a running total...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.