SQL query to compare product sales by month

后端 未结 6 1687
忘掉有多难
忘掉有多难 2021-02-10 07:45

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
B         


        
6条回答
  •  囚心锁ツ
    2021-02-10 08:35

    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

提交回复
热议问题