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

  • SEARCH
  • Home
  • 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 6223843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:33:40+00:00 2026-05-24T08:33:40+00:00

I wanted to play around with my Total_Sales table. This is how the data

  • 0

I wanted to play around with my Total_Sales table.
This is how the data looks like (using SQL Server 2008 R2)

Name    Year  Month  Sales
------  ----  -----  -----
Alfred  2011  1      100
Alfred  2011  2      200
Alfred  2011  3      300
Alfred  2011  4      400
Alfred  2011  5      500
Alfred  2011  6      600
Alfred  2011  7      700
Alfred  2011  8      800
Alfred  2011  9      900
Alfred  2011  10     500
Alfred  2011  11     500
Alfred  2011  12     500

The SQL query I want to create should display the data like this:

Name    Year  Month  Sales Prev_Month Month_Last_Year_Sales Last_12_Month_AVG
------  ----  -----  ----- ---------- --------------------- -----------------
Alfred  2011  1      100   NULL       (year 2010, month 1)  (2010_01 to 2011_01)/(12)
Alfred  2011  2      200   100        (year 2010, month 2)  (2010_02 to 2011_02)/(12)
Alfred  2011  3      300   200        (year 2010, month 3)  (2010_03 to 2011_03)/(12)
Alfred  2011  4      400   300        (year 2010, month 4)  (2010_04 to 2011_04)/(12)
Alfred  2011  5      500   400        (year 2010, month 5)  (2010_05 to 2011_05)/(12)
Alfred  2011  6      600   500        (year 2010, month 6)  (2010_06 to 2011_06)/(12)
Alfred  2011  7      700   600        (year 2010, month 7)  (2010_07 to 2011_07)/(12)
Alfred  2011  8      800   700        (year 2010, month 8)  (2010_08 to 2011_08)/(12)
Alfred  2011  9      900   800        (year 2010, month 9)  (2010_09 to 2011_09)/(12)
Alfred  2011  10     500   900        (year 2010, month 10) (2010_10 to 2011_10)/(12)
Alfred  2011  11     500   500        (year 2010, month 11) (2010_11 to 2011_11)/(12)
Alfred  2011  12     500   500        (year 2010, month 12) (2010_12 to 2011_12)/(12)

To copy the prior month I am using this: Copy prior month value and insert into new row

SELECT
TS.name,
TS.year,
TS.month,
TS.sales,
COALESCE(TS2.sales, 0) AS prior_month_sales
FROM
TotalSales TS
LEFT OUTER JOIN TotalSales TS2 ON
TS2.name = TS.name AND
(
    (TS2.year = TS.year AND TS2.month = TS.month - 1) OR
    (TS.month = 1 AND TS2.month = 12 AND TS2.year = TS.year - 1)
)

The NULL in Prev_Month is to show that the start of the Total_Sales was in year 2011 month 1, so no prior data for this example.

I am planning to use a parameter, where you select a month.

Thanks for any help!

  • 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. Editorial Team
    Editorial Team
    2026-05-24T08:33:40+00:00Added an answer on May 24, 2026 at 8:33 am
    SELECT
      [this_month].*,
      [last_month].Sales        AS [prev_month_sales],
      [last_year].Sales         AS [month_last_year_sales],
      [yearly].AverageSales     AS [last_12_month_average]
    FROM
      Total_Sales     AS [this_month]
    LEFT JOIN
      Total_Sales     AS [last_month]
        ON  [last_month].Name = [this_month].Name
        AND (
             ([last_month].Year = [this_month].Year     AND [last_month].Month = [this_month].Month - 1)
          OR ([last_month].Year = [this_month].Year - 1 AND [last_month].Month = 12 AND [this_month].Month = 1)
        )
    LEFT JOIN
      TotalSales     AS [last_year]
        ON  [last_year].Name  = [this_month].Name
        AND [last_year].Year  = [this_month].Year - 1
        AND [last_year].Month = [this_month].Month
    CROSS APPLY
    (
      SELECT
        AVG(Sales) AS AverageSales
      FROM
        Total_Sales
      WHERE
        Name = [this_month].Name
        AND (
                (Year = [this_month].Year     AND Month <= [this_month].Month)
             OR (Year = [this_month].Year - 1 AND Month >  [this_month].Month)
        )
    )
      AS [yearly]
    

    The Average isn’t the value divided by 12, as there are not always 12 months worth of data in the preceding year. But the AVG() function takes care of that for you.

    Also, I’d highly reccomend against using YEAR and MONTH fields. Instead I would recommend using a DATETIME field to represent the “Month Start” and using SQL Server’s Date functions…

    Last Month : MonthStart = DATEADD(MONTH, -1, ThisMonth)
    A Year Ago : MonthStart = DATEADD(YEAR,  -1, ThisMonth)
    Last Year  : MonthStart > DATEADD(YEAR,  -1, ThisMonth) AND MonthStart <= ThisMonth
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wanted to play around with the jdk . Like changing the implementation
I wanted to play around with a single table inheritance approach in one of
I've been trying to get started using Cocos2D-X, because I wanted to play around
I'm brand new to node.js, but I wanted to play around with some basic
I'm just starting to play around with GTK+ and I wanted to stop bad
I never used ruby before, I just wanted to play around with HAML and
I am using LWJGL's controller class to connect my controller and play around. I
Ok so I wanted to play around with CCBlade https://github.com/hiepnd/CCBlade and in the installation
I'm playing around with redis and wanted to recreate a table I have in
I wanted to play around with Linux and do some testing. I've tried installing

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.