Jump to content

[SOLVED] Need query to pull rows for current year but...


5kyy8lu3

Recommended Posts

 

Hi.    I have PHP 5.0.45.

 

I know I can somehow do this with PHP but I'm slowing realizing how powerful MySQL can be on its own and would like to see if there's a way I can do this using just a complex query.  I would like to pull ALL rows from my table from the current year BUT I want any rows from the same day (according to the timestamp) to be averaged into one value so i end up with 365 entries for the year (assuming they have at least one entry per day).

 

Is this possible?  I tried to play around with using GROUP BY and AVG but I can't seem to get it right, any advice/suggestions would be great.  If all else fails I'll just pull everything and do the work with php.  Thanks. :)

 

 

Link to comment
Share on other sites

To get results for the current year, you would need -

 

WHERE YEAR(DateAdded) = YEAR(CURDATE())

 

and if that does not work because YEAR() expects a date, try this -

 

WHERE YEAR(DATE(DateAdded)) = YEAR(CURDATE())

 

well I know how to pull rows from this year, what I'm having trouble doing is bringing back 1 result per day, so I need 365 results back.  And if there are multiple rows for one day, I need it to average those into one result for that date.  I'm not sure if this makes sense lol.

 

"SELECT AVG(Speed) FROM MyTable GROUP BY DATE(DateAdded) WHERE YEAR(DateAdded) = YEAR(CURDATE()) ORDER BY DateAdded ASC"

 

Ok pretend table:

 

DateAdded  -    Speed

 

2009-02-01        0

2009-02-01        10

2009-02-02        6

2009-02-03        2

2009-02-03        4

 

And I want this as my result:

 

2009-02-01        5

2009-02-02        6

2009-02-03        3

Link to comment
Share on other sites

Once you put the WHERE clause before the GROUP BY clause, it works for me -

 

DateAdded  AVG(Speed)

2009-02-01  5.0000

2009-02-02  6.0000

2009-02-03  3.0000

 

If you tried something and it did not work, you would need to post your query/code for anyone to be able to help with what it was doing wrong.

Link to comment
Share on other sites

Once you put the WHERE clause before the GROUP BY clause, it works for me -

 

DateAdded  AVG(Speed)

2009-02-01  5.0000

2009-02-02  6.0000

2009-02-03  3.0000

 

If you tried something and it did not work, you would need to post your query/code for anyone to be able to help with what it was doing wrong.

 

yea i tried it and it didn't work until i put the where clause first and it works now, thanks for the help =)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.