Show missing days for aggregated data across a date range

Posted on 2009-02-23
Last Modified: 2012-05-06
I have a table called "orders" that contains a unique ID "orderid", a datestamp field "ctime" and a money field "profit"

I want to pull out all the data for february 2009 with the profit aggregated using SUM, but I want to show days with no data even when there are no orders on those dates.

With the following data:

ORDERID  CTIME                PROFIT
122          31 jan 2009       $750
123          01 feb 2009       $1000
124          01 feb 2009       $1500
125          03 feb 2009       $1600
126          05 feb 2009       $500
127          06 feb 2009       $1000

if I use the following code:

select datepart(day,ctime) as 'day', sum(profit) as profit from orders where datepart(month,ctime) = 2 and datepart(year,ctime) = 2009 group by datepart(day,ctime) order by datepart(day,ctime)

I get :

1      $2500
3      $1600
5      $500
6      $1000

What I want is:

1      $2500
2      $0
3      $1600
4      $0
5      $500
6      $1000

I tried creating a table with numbers 1-28 in it and joining to this, but this didn't produce the desired effect.

Any assistance would be appreciated.
Question by:neburton
    LVL 142

    Accepted Solution

    you need to left/right join with that table:
    select, s.profit
     from your_day_listing t
     left join (select datepart(day,ctime) as [day], sum(profit) as profit from orders where datepart(month,ctime) = 2 and datepart(year,ctime) = 2009 group by datepart(day,ctime)) s
       on t.[day] = s.[day] 
     order by t.[day]

    Open in new window

    LVL 1

    Author Closing Comment

    That solved it.  Many thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now