Connecting Tech Pros Worldwide Forums | Help | Site Map

Query to find the difference b/w averages

Member
 
Join Date: Jul 2007
Posts: 38
#1: Dec 18 '08
Hi,

I have wrote two queries, one to find the average for one date, and the second query to find the average for another date. And I stored the results of the query to a local string object. Is there anyway to compare the two averages by getting the difference between the two.

example query
Expand|Select|Wrap|Line Numbers
  1.  
  2. String myQuery = "" +
  3.         "select name, age, date, avg(count) from product where date=interval -7 Day + '2001-08-13'  " +
  4.         " group by name "; 
  5.  
Let's say that I get another query for the next 7 days, how do I compare the two averages from the two queries?

Note: Using Mysql 5.0, and JDBC app to connect to DB, table, etc (works fine)
just need help with the query in my program
Thanks,

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Dec 19 '08

re: Query to find the difference b/w averages


Why not just compare the values that you have stored in those local string "objects" that you saved them to?
Member
 
Join Date: Jul 2007
Posts: 38
#3: Dec 29 '08

re: Query to find the difference b/w averages


Thanks for the quick response, but how do I accomplish that? Can you provide a code snippet, or refer me to an article?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Dec 30 '08

re: Query to find the difference b/w averages


You convert a string to a number using Integer.parseInt. You then compare the integers using the normal comparison operators.
Member
 
Join Date: Jul 2007
Posts: 38
#5: Dec 30 '08

re: Query to find the difference b/w averages


Thanks, I was able to make the conversion from string to int. But my next question is how do I write the sql query to query the database to return the results using the new averages in which I converted to integers?


//newdailyCount is a string like "1.2222"
//firstAverage is an int

Expand|Select|Wrap|Line Numbers
  1. firstAverage=Integer.parseInt(newdailyCount);
Here is the query I tried to return the results after I get the avg of the previous seven days, and the next seven days, then I want to subtract the avgs of the two. But the query doesn't work.

Expand|Select|Wrap|Line Numbers
  1. # String myQuery = "" +
  2. #         "select name, age, date, avg(count) from product where newdailyCount=(firstAverage -10) " +
  3. #         " group by name "; 
Thanks,
Member
 
Join Date: Jul 2007
Posts: 38
#6: Jan 12 '09

re: Query to find the difference b/w averages


Can someone provide a code snippet or suggestions for the query above?

thanks,
Reply