RE: difficult count statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> > SELECT     DATE_FORMAT( date_column, '%Y- %m- %d') AS mydate,
> >                    COUNT(*) AS c
> > FROM         table
> > GROUP BY mydate
> 
> This wokes. Thank you ignatius! I have worked on the charts and it
helped
> me
> to get a lot further.
> There is a point where I still have not found a good solution.
> 
> Problem is, that there are some days where no data has changed (for
> example
> a day with no new registered members).
> In this case the query should return '0'
> 
> E.g.:
> Jan    10
> Feb   32
> Mar   0
> Apr   5
> 
> At the moment I would not have march listed in my chart which causes
> problems.

I'd just handle the empty months in PHP.

1. Issue your query to retrieve the months you want, ordered by month
ascending. 

2. Retrieve first row containing month and count

3. Create a PHP loop that starts at the first month's timestamp until
the last month.

4. Compare the month of the loop with the month retrieved from the
database. If they match, output the number and fetch the next row. If
they do not match, output zero, do not fetch another row and move on to
the next month in your PHP loop.

5. Continue until done. 

This way you don't have to create and maintain a dummy database with
months/dates in it and your query will be less complex. 

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux