SQL query to compare product sales by month

I have a Monthly Status database view I need to build a report based on. The data in the view looks something like this:

Category | Revenue  |  Yearh  |  Month
Bikes      10 000      2008        1
Bikes      12 000      2008        2
Bikes      12 000      2008        3
Bikes      15 000      2008        1
Bikes      11 000      2007        2
Bikes      11 500      2007        3
Bikes      15 400      2007        4


... And so forth

The view has a product category, a revenue, a year and a month. I want to create a report comparing 2007 and 2008, showing 0 for the months with no sales. So the report should look something like this:

Category  |  Month  |  Rev. This Year  |  Rev. Last Year
Bikes          1          10 000               0
Bikes          2          12 000               11 000
Bikes          3          12 000               11 500
Bikes          4          0                    15 400


The key thing to notice is how month 1 only has sales in 2008, and therefore is 0 for 2007. Also, month 4 only has no sales in 2008, hence the 0, while it has sales in 2007 and still show up.

Also, the report is actually for financial year - so I would love to have empty columns with 0 in both if there was no sales in say month 5 for either 2007 or 2008.

The query I got looks something like this:

SELECT 
    SP1.Program,
    SP1.Year,
    SP1.Month,
    SP1.TotalRevenue,
    IsNull(SP2.TotalRevenue, 0) AS LastYearTotalRevenue

FROM PVMonthlyStatusReport AS SP1 
     LEFT OUTER JOIN PVMonthlyStatusReport AS SP2 ON 
                SP1.Program = SP2.Program AND 
                SP2.Year = SP1.Year - 1 AND 
                SP1.Month = SP2.Month
WHERE 
    SP1.Program = 'Bikes' AND
    SP1.Category = @Category AND 
    (SP1.Year >= @FinancialYear AND SP1.Year <= @FinancialYear + 1) AND
    ((SP1.Year = @FinancialYear AND SP1.Month > 6) OR 
     (SP1.Year = @FinancialYear + 1 AND SP1.Month <= 6))

ORDER BY SP1.Year, SP1.Month

The problem with this query is that it would not return the fourth row in my example data above, since we didn't have any sales in 2008, but we actually did in 2007.

This is probably a common query/problem, but my SQL is rusty after doing front-end development for so long. Any help is greatly appreciated!

Oh, btw, I'm using SQL 2005 for this query so if there are any helpful new features that might help me let me know.

This question and answers originated from www.stackoverflow.com
Question by (8/19/2008 11:24:47 PM)

Answer

The Case Statement is my best sql friend. You also need a table for time to generate your 0 rev in both months.

Assumptions are based on the availability of following tables:

sales: Category | Revenue | Yearh | Month

and

tm: Year | Month (populated with all dates required for reporting)

Example 1 without empty rows:

select
    Category
    ,month
    ,SUM(CASE WHEN YEAR = 2008 THEN Revenue ELSE 0 END) this_year
    ,SUM(CASE WHEN YEAR = 2007 THEN Revenue ELSE 0 END) last_year

from
    sales

where
    year in (2008,2007)

group by
    Category
    ,month

RETURNS:

Category  |  Month  |  Rev. This Year  |  Rev. Last Year
Bikes          1          10 000               0
Bikes          2          12 000               11 000
Bikes          3          12 000               11 500
Bikes          4          0                    15 400

Example 2 with empty rows: I am going to use a sub query (but others may not) and will return an empty row for every product and year month combo.

select
    fill.Category
    ,fill.month
    ,SUM(CASE WHEN YEAR = 2008 THEN Revenue ELSE 0 END) this_year
    ,SUM(CASE WHEN YEAR = 2007 THEN Revenue ELSE 0 END) last_year

from
    sales
    Right join (select distinct  --try out left, right and cross joins to test results.
                   product
                   ,year
                   ,month
               from
                  sales --this ideally would be from a products table
                  cross join tm
               where
                    year in (2008,2007)) fill


where
    fill.year in (2008,2007)

group by
    fill.Category
    ,fill.month

RETURNS:

Category  |  Month  |  Rev. This Year  |  Rev. Last Year
Bikes          1          10 000               0
Bikes          2          12 000               11 000
Bikes          3          12 000               11 500
Bikes          4          0                    15 400
Bikes          5          0                    0
Bikes          6          0                    0
Bikes          7          0                    0
Bikes          8          0                    0

Note that most reporting tools will do this crosstab or matrix functionality, and now that i think of it SQL Server 2005 has pivot syntax that will do this as well.

Here are some additional resources. CASE http://www.4guysfromrolla.com/webtech/102704-1.shtml SQL SERVER 2005 PIVOT http://msdn.microsoft.com/en-us/library/ms177410.aspx

Answer by

Find More Answers
Related Topics  sql  sql-server  database  query  reporting
Related Questions
  • LFL sales query in SQL

    This might seem a bit like a do-my-homework-for-me question (and it is), but I think that the sales results I’m trying to get are fairly generic and will be useful for anyone doing sales reporting. …
  • SQL query for displaying product sales

    I have sales table which consists, ItemSize, GroupName, Quantity, ProductID, etc... Now I want to display sales according to following format GroupName ItemSize Quantity ItemSize Quantity …
  • SQL query for Figuring counts by month

    whats a good way to join 1-12 in a column to a bunch of counts by month?... in SQL SELECT months???, count(whatever1) count1, count(whatever2) count2 FROM months???? LEFT JOIN whatever1…
  • SQL Query For Product Sales Report

    I have a query that gives a product sales report by whatever date range I specify. Something like select whatever from wherever where date ordered between start date and end date order by product…
  • SQL query: Get the most expensive product ordered in each month of 2011

    I don't understand how to "combine" two tables so that I can generate an answer to this. Basically I have a CAP database and I'm only concerned with two tables: ORDERS (ordno, month, year, cid…
  • SQL Server query

    I have a SQL Server DB containing a registrations table that I need to plot on a graph over time. The issue is that I need to break this down by where the user registered from (e.g. website, wap sit…
  • sql query to compare

    I want to show 'Pass' if marks of the student are 33 or above and 'Fail' if they are less than 33. My table is id,rollno,subject,marks what i want to show is record: id rollno subject marks…
  • SQL Query to compare

    I am working on an Access database and in it I need to extract several reports. For one of them I need to compare two lists of numbers, List1 and List2(I get both from separate queries) and display …
  • SQL query to identify seasonal sales items

    I need a SQL query that will identify seasonal sales items. My table has the following structure - ProdId WeekEnd Sales 234 23/04/09 543.23 234 30/04/09 12.43 432 23/04/09 0.0…
  • SQL Server - SSRS - Where to find query generating the report

    The guy who created all the reporting using SSRS has left, and I am trying to troubleshoot one of the reports being generated. How do I get access to look at the query that is generating the report.…