MySQL Forums
Forum List  »  Newbie

Re: Forcing COUNT() to return value even when no records exist
Posted by: Rick James
Date: August 26, 2009 11:08PM

JOIN to a simple table that contains weekNum values (or simply numbers).
Let's say you create table Numbers and populate it with 1, 2, ... 1000.
Then something like this will do the trick:
SELECT  COUNT(b.bookingID) AS number, WEEK(b.date) AS weekNum
    FROM  bookings b
    JOIN  Numbers n ON WEEK(b.date) = n.number
    WHERE  n.number <= WEEK(DATE_ADD(CURDATE(), INTERVAL -1 WEEK))
      AND  n.number >= WEEK(DATE_ADD(CURDATE(), INTERVAL -9 WEEK))
    GROUP BY  n.number

Have fun in January.

Options: ReplyQuote


Subject
Written By
Posted
Re: Forcing COUNT() to return value even when no records exist
August 26, 2009 11:08PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.