473,406 Members | 2,713 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,406 software developers and data experts.

AVG between two dates

Hi,

I am trying to figure out how to find the avg in between two dates in the work_date field. For example, let's say I want the avg from 2007-01-24 to 2007-05-06, I would have to find the avg by taking the values in the daily_typing_pages columns and add up all the columns between the the dates provided. (E.G. (100+220+350+250+170)/5).
Expand|Select|Wrap|Line Numbers
  1. +------+------+------------+--------------------+
  2. | id   | name | work_date  | daily_typing_pages |
  3. +------+------+------------+--------------------+
  4. |    1 | John | 2007-01-24 |                250 |
  5. |    2 | Ram  | 2007-05-27 |                220 |
  6. |    3 | Jack | 2007-05-06 |                170 |
  7. |    3 | Jack | 2007-04-06 |                100 |
  8. |    4 | Jill | 2007-04-06 |                220 |
  9. |    5 | Zara | 2007-06-06 |                300 |
  10. |    5 | Zara | 2007-02-06 |                350 |
  11. +------+------+------------+--------------------+
  12.  
this is what i tried but it returns empty set.

Expand|Select|Wrap|Line Numbers
  1. select name, avg(daily_typing_pages) 
  2. from product 
  3. where work_date between '2007-01-24' and '2007-05-06' 
  4. group by name
  5.  
This is really important any help is appreciated
Dec 11 '08 #1
5 5081
Atli
5,058 Expert 4TB
Hi.

I tested this over on my server and it worked just fine.
This is what I did:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE t1 (
  2.   name VarChar(255) Not Null,
  3.   work_date DateTime Not Null,
  4.   daily_typing_pages int Not Null
  5. );
  6.  
  7. INSERT INTO t1 (name, work_date, daily_typing_pages)
  8. VALUES
  9. ('John', '2007-01-24',  250),
  10. ('Ram ', '2007-05-27',  220),
  11. ('Jack', '2007-05-06',  170),
  12. ('Jack', '2007-04-06',  100),
  13. ('Jill', '2007-04-06',  220),
  14. ('Zara', '2007-06-06',  300),
  15. ('Zara', '2007-02-06',  350);
  16.  
  17. SELECT name, AVG(daily_typing_pages) 
  18. FROM t1 
  19. WHERE work_date BETWEEN '2007-01-24' AND '2007-05-06' 
  20. GROUP BY name
  21.  
And I got these results:
Expand|Select|Wrap|Line Numbers
  1. +------+-------------------------+
  2. | name | AVG(daily_typing_pages) |
  3. +------+-------------------------+
  4. | Jack |                135.0000 |
  5. | Jill |                220.0000 |
  6. | John |                250.0000 |
  7. | Zara |                350.0000 |
  8. +------+-------------------------+
  9. 4 rows in set (0.00 sec)
Doesn't seem to be a problem with your query.

P.S.
Please use [code] tags when posting your code examples.
(See How to ask a question)

[code] ... Code goes here... [/code]

Thank you.
Moderator
Dec 12 '08 #2
Thanks for the quick response,

Well its still not working for me, The field with the dates are of date type, do you think that's the problem? If so, how do I resolve the issue and return the query you have above?

Thanks,
Dec 12 '08 #3
Atli
5,058 Expert 4TB
Whether it's a Date or DateTime field should make no difference at all. The date part of the data would still be the same.

The query itself works, so the problem must be somewhere else.
We need more info if we are to be able to help you with this.

Could you post the CREATE TABLE statement you used to create your table?
(You can get from MySQL by doing SHOW CREATE TABLE)

What version of MySQL are you running?
Are you executing the query through some API?

Anything else that might be having an effect on this?
Dec 12 '08 #4
I am using MYSQL version 5.0 and I am connecting to a table that was already created with the appropriate fields, and I am using JDBC to connect to the instance and tables which connects and works fine. The only problem is when I try and run the query is where I don't get any results.


Expand|Select|Wrap|Line Numbers
  1.   SELECT name, AVG(daily_typing_pages) 
  2. FROM t1 
  3.  WHERE work_date BETWEEN '2007-01-24' AND '2007-05-06' 
  4. GROUP BY name 
Thanks
Dec 12 '08 #5
Also here is code snippet of jdbc prog

Expand|Select|Wrap|Line Numbers
  1. String url = "jdbc:mysql://MyDriver/";
  2.         String dbName="product";
  3.         String driver="com.mysql.jdbc.Driver";
  4.         String username="root";
  5.         String password="password";
  6.         String myQuery="SELECT name, AVG(daily_typing_pages) 
  7.  FROM t1 
  8.   WHERE work_date BETWEEN '2007-01-24' AND '2007-05-06' 
  9.  GROUP BY name ";
  10.  
  11.  
  12.  
  13.         try
  14.         {
  15.             Class.forName(driver).newInstance();
  16.             connect =   DriverManager.getConnection(url+dbName,username,password);
  17.             Statement state=connect.createStatement();        
  18.  
  19.               ResultSet result=state.executeQuery(myQuery);
  20.                 while (result.next())
  21.                 {                
  22.                 //do something
  23. }
  24.  
Thanks,
Dec 12 '08 #6

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

Similar topics

8
by: Riley | last post by:
The date fields being saved by a VB program were being saved as #2003-11-22#. For reasons unknown to me these dates began to be saved as "11/22/2003" All of these dates were made dates with the...
7
by: Alistair | last post by:
diary_date = request.form("diary_date") - (from a populated drop down list) strSQL = "SELECT saz_title, saz_text from saz_details where saz_date =#" & diary_date & "#" a response.write...
5
by: PW | last post by:
<rant> Sorry guys, but I just have to whinge. Dates in ASP are a total pain in the butt! I seem to get caught out so many times. I realise its my own fault, but going from the posts in this...
10
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: ...
1
by: Don Sealer | last post by:
I have a report that includes 5 different subreports. I'd like to be able to open this report using a date function (Start Date and End Date). I'd like all five subreports to show the data from...
2
by: Rachel Suddeth | last post by:
Is there a way to have the non-selectable dates (those before MinDate and after MaxDate) draw differently so my users can see right away what dates aren't allowed? I'm not seeing it... ...
12
by: Dixie | last post by:
I am trying to calculate the number of workdays between two dates with regards to holidays as well. I have used Arvin Meyer's code on the Access Web, but as I am in Australia and my date format is...
1
by: pitfour.ferguson | last post by:
My dbase has the start date and end date of each visit. How can I ask Access to list the day of the week of the start (easy), end (easy) and, more importantly, the dates of the visit itself - ie...
7
by: evilcowstare via AccessMonster.com | last post by:
Hi, I have searched the forum for answers on this and to be honest as a novice I find it a bit confusing so apologies if it is simple. There are some searches that I want to apply to my database....
2
by: Jim Carlock | last post by:
(1) Does PHP provide any way to handle dates prior to 1980? I know there's problems with Microsoft Windows NT and all Windows NT operating systems will allow a date prior to 1980 to be placed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.