General sql query question (compare date ranges)

Dave_beDave_be Member Posts: 35
edited 2009-10-07 in SQL General
I'm trying to create a report true SQL BI that compares the sales of a product/customer between two given date frames.
For example, I want to see the difference in sale from item "111" from (01/01/2008 till 31/12/2008) and (01/01/2009 until 31/01/09.)
All data is in the same table, so no external joins are needed.

Data output must give something like this:
Item     Date 1       Date 2      Difference
111       2000           1000           -100% 
But the second thing is, when date 1 has no data for the period it still must show date 2.
Item   Date 1      Date 2      Difference
111       0            1000          100% 
I just can't seem to get this in my head and this is really frustrating me for quite some time now.
Any help would be highly appreciated

Comments

  • bbrownbbrown Member Posts: 3,268
    Use a left outer join between the two parts of your query. This way it returns an item only if it has data for the first range.
    There are no bugs - only undocumented features.
Sign In or Register to comment.