Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 36059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:20:02+00:00 2026-05-10T14:20:02+00:00

I have a Monthly Status database view I need to build a report based

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:20:02+00:00Added an answer on May 10, 2026 at 2:20 pm

    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 https://web.archive.org/web/20210728081626/https://www.4guysfromrolla.com/webtech/102704-1.shtml SQL SERVER 2005 PIVOT http://msdn.microsoft.com/en-us/library/ms177410.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 68k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer First thoughts: toss away noise words (and, you, is, the,… May 11, 2026 at 12:25 pm
  • added an answer I think you want date_select instead of select_date, since you… May 11, 2026 at 12:25 pm
  • added an answer There's a class called DriveDetector over at Codeproject that sounds… May 11, 2026 at 12:25 pm

Related Questions

I have a web-service that I will be deploying to dev, staging and production.
I have a .Net desktop application with a TreeView as one of the UI
Is it possible to make a simple query to count how many records I
I have a ToggleButtonBar with a DataProvider setup like this: <mx:ToggleButtonBar itemClick=clickHandler(event); selectedIndex=0> <mx:dataProvider>

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.