Horizontal Axis Month Sales

  • Hi

    When you have a horizontal axis looking at sales by month on a graph, how can you show an month with no sales ?

    Paul

  • You may need to force the existence of all dates in the range. (This would probably be better with a CTE to generate dates in a range, but anyway...)

    -- create calendar table

    CREATE TABLE Sales.Calendar(

    calendardate datetime

    CONSTRAINT pkCalendar PRIMARY KEY (calendarDate));

    -- populate the calendar table

    DECLARE @theDate datetime = '2007-02-01';

    WHILE @theDate<'2007-02-28'

    BEGIN

    INSERT INTO Sales.Calendar(calendarDate) VALUES (@theDate);

    SET @theDate = DATEADD(d,1,@theDate)

    END

    -- outer join calendar table to sales to force existence of all dates in the range.

    SELECT c.CalendarDate

    , COUNT(o.OrderID) SalesCount

    , COALESCE(SUM(o.Freight),0) TotalFreight

    FROM Sales.Calendar c LEFT JOIN Sales.Orders o

    ON c.calendardate = o.orderdate

    GROUP BY c.calendardate;

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply