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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:44:01+00:00 2026-05-23T14:44:01+00:00

I need to build a query with 4 columns (sql 2005). Column1: Product Column2:

  • 0

I need to build a query with 4 columns (sql 2005).

Column1: Product
Column2: Units sold
Column3: Growth from previous month (in %)
Column4: Growth from same month last year (in %)

In my table the year and months have custom integer values. For example, the most current month is 146 – but also the table has a year (eg 2011) column and month (eg 7) column.

Is it possible to get this done in one query or do i need to start employing temp tables etc??

Appreciate any help.

thanks,

KS

  • 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-23T14:44:02+00:00Added an answer on May 23, 2026 at 2:44 pm

    KS,
    To do this on the fly, you could use subqueries.

    SELECT product, this_month.units_sold,
        (this_month.sales-last_month.sales)*100/last_month.sales,
        (this_month.sales-last_year.sales)*100/last_year.sales
        FROM (SELECT product, SUM(units_sold) AS units_sold, SUM(sales) AS sales
                FROM product WHERE month = 146 GROUP BY product) AS this_month,
             (SELECT product, SUM(units_sold) AS units_sold, SUM(sales) AS sales
                FROM product WHERE month = 145 GROUP BY product) AS last_month,
             (SELECT product, SUM(units_sold) AS units_sold, SUM(sales) AS sales
                FROM product WHERE month = 134 GROUP BY product) AS this_year
        WHERE this_month.product = last_month.product
          AND this_month.product = last_year.product
    

    If there’s a case where a product was sold in one month but not another month, you will have to do a left join and check for null values, especially if last_month.sales or last_year.sales is 0.

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

Sidebar

Related Questions

I need some help to build SQL Query. I have table having data like:
I need some help to build SQL Query. I have table having data like:
I need to build a MySQL query that returns only the common columns for
Hi I need to build a table with two columns from a mySQL table.
I need to build a dynamic linq query with or operators. I have seen
I need to build a function which parses the domain from a URL. So,
I need to build a sqlserver query that if a text column has a
I have a particularly complicated query for a report. It selects several columns from
I want to build a single select stored procedure for SQL 2005 that is
I am using sql server 2008 and I'm trying to build a query for

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.