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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:33:21+00:00 2026-06-03T17:33:21+00:00

I have a table, called #MonthlySales, that pulls up ItemNumber, MonYearNum & UnitsSold. I

  • 0

I have a table, called #MonthlySales, that pulls up ItemNumber, MonYearNum & UnitsSold. I pivoted this with the following script:

SELECT 
    ITEMNUMBER [SKU]
    , ISNULL([1-2011], 0) [Jan 11]
    , ISNULL([2-2011], 0) [Feb 11]
    , ISNULL([3-2011], 0) [Mar 11]
    , ISNULL([4-2011], 0) [Apr 11]
    , ISNULL([5-2011], 0) [May 11]
    , ISNULL([6-2011], 0) [Jun 11]
    , ISNULL([7-2011], 0) [Jul 11]
    , ISNULL([8-2011], 0) [Aug 11]
    , ISNULL([9-2011], 0) [Sep 11]
    , ISNULL([10-2011], 0) [Oct 11]
    , ISNULL([11-2011], 0) [Nov 11]
    , ISNULL([12-2011], 0) [Dec 11] 

, ISNULL([1-2012], 0) [Jan 12]
, ISNULL([2-2012], 0) [Feb 12]
, ISNULL([3-2012], 0) [Mar 12]
, ISNULL([4-2012], 0) [Apr 12]
, ISNULL([5-2012], 0) [May 12]
, ISNULL([6-2012], 0) [Jun 12]
, ISNULL([7-2012], 0) [Jul 12]
, ISNULL([8-2012], 0) [Aug 12]
, ISNULL([9-2012], 0) [Sep 12]
, ISNULL([10-2012], 0) [Oct 12]
, ISNULL([11-2012], 0) [Nov 12]
, ISNULL([12-2012], 0) [Dec 12] 

FROM
#MONTHLYSALES
PIVOT
(
SUM(UNITSSOLD) FOR MONTHYEARNUM IN
(
[1-2011], [2-2011], [3-2011], [4-2011], [5-2011], [6-2011], [7-2011], [8-2011], [9-2011], [10-2011], [11-2011], [12-2011]
,[1-2012], [2-2012],
[3-2012], [4-2012], [5-2012], [6-2012], [7-2012], [8-2012], [9-2012], [10-2012], [11-2012], [12-2012]
)
) AS TOTSOLDPERMONTH
ORDER BY
ITEMNUMBER
As you can see, the MonthYearNum column is hardcoded into values like 1-2011. However, I cannot hardcode these, since they are dynamic – depending on which day this report is ran, different MonthYearNum values will be returned from #monthlysales table.

Is there a way for me to dynamically pivot the values that are returned?

TIA

  • 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-06-03T17:33:22+00:00Added an answer on June 3, 2026 at 5:33 pm

    Yep–write dynamic SQL. It’s complex, and rather than rewrite similar code (’cause I’m lazy), please refer to my answer to this question for a sample. It’s code for a dynamic unpivot, but the concept is the same.

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

Sidebar

Related Questions

I have a table called tblRentalRates with the following columns: rate_id (int) rate_startdate (datetime)
I have table called Direccion other called Cliente each one defined like this public
Hi I have table called Sold Products which stores buying information for product &
I have a table called employee with following fields Id | Name | PrimaryEmail1
I have a table called USERS that has a foreign key to the table
I have a table called Movies i want to select some columns from it
I have table called Reporting with following columns OutletId CampaignId ItemId Qty 10 1
In my db I a have table called users. In that table I have
I have table name called Person with following column names P_Id(int), LastName(varchar), FirstName (varchar).
I have a table called user_details this table has got morethan 100k user details

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.