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 6228289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:21:48+00:00 2026-05-24T09:21:48+00:00

Here is an example of the current table I have: 1) Table name: TotalSales

  • 0

Here is an example of the current table I have:

1) Table name: TotalSales

Name    Year  Month  Sales
------  ----  -----  -----
Alfred  2011  1      100

What I want to do is create a table like this, add a new row(Prior month sales):

2) Table name: TotalSales

Name    Year  Month  Sales  Prior month sales
------  ----  -----  -----  -----------------
Alfred  2011  2      110    100

Not sure how to this, but this is what I have been working on:

SELECT Name, Year, Month, Sales, Sales as [Prior Month sales]
FROM TotalSales
WHERE
DATEPART(month, [Prior Month sales]) = DATEPART(month, DATEADD(month, -1, getdate()))

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-24T09:21:49+00:00Added an answer on May 24, 2026 at 9:21 am

    To select the given data you need to join the table to itself:

    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 LEFT OUTER JOIN is an outer join in case they didn’t have any sales the previous month (or this is their first month with the company).

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

Sidebar

Related Questions

Here an example http://jsfiddle.net/7aVXc/ Let' say current image height is 400px . How do
Here is my current code: RewriteCond %{HTTP_HOST} !^example\.com [NC] #RewriteCond %{REQUEST_URI}!^something RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
Here is example which I have try <?php include 'spider/classes/simple_html_dom.php'; $html = new simple_html_dom();
I have an example here: int runcmd(char *cmd) { char* argv[MAX_ARGS]; pid_t child_pid; int
I have a table with details on personnel. I would like to create a
I have a table like : trans is the name of the table for
I have 3 entities in an example app: Account name balance (NSDecimalNumber overall balance
We have a SQL Server table containing Company Name, Address, and Contact name (among
I want to create a DB table and a set of commands for it.
Here an example http://jsfiddle.net/EhLsT/ $(window).scroll(function () { if ($(window).scrollTop() > $(#header).offset().top) { $(#floating).show(); }

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.