Click to See Complete Forum and Search --> : Select max value from database


halldorr
10-23-2008, 01:08 PM
Not sure if I can do this directly with a SQL query or if I'll need to manipulate it in PHP. So would love to get some second thoughts on it. Currently I'm leaning towards just grabbing it all into a lage array and manipulating it in PHP but it feels like a poor method to use so am hoping perhaps some people may have some thoughts on it.

I have a table that is set up like:

date_logged|max_value

So an example is : 2008-08-30|1000

There is an entry for everyday and I am trying to run a query that will return the max for each month

So in the end I'd love an array that would return the max for each month but not sure if it's possible with just SQL or if I'll need to hack it and make it individual queries (which I'd pefer not to since it would involve several queries).

Ultimately I need to pull the max value for each month and display that in an array along with the month/year.

philmee95
10-23-2008, 05:54 PM
SELECT count( * ) , MOnth( datetime_field)
FROM `table`
WHERE 1 =1
GROUP BY MOnth( datetime_field)

philmee95
10-23-2008, 05:56 PM
limit results using having

SELECT count(*),MOnth( tstamp) as monthy FROM `table` WHERE 1=1
GROUP BY MOnth( tstamp)
having monthy = 10

only returns octobers