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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:33:06+00:00 2026-05-30T03:33:06+00:00

I’m attempting to transform this data: ItemID MonthAsInt Month Year InvType Quantity 4643 4

  • 0

I’m attempting to transform this data:

ItemID  MonthAsInt  Month     Year  InvType     Quantity
4643    4           April     2011  Shipment    10
4643    5           May       2011  Shipment    10
4643    7           July      2011  Shipment    10
4643    8           August    2011  Destroy     10
4643    11          November  2011  Shipment    25
4643    12          December  2011  Picking     1

Into this (basically, a 12 month snap shot):

              February    March    April     May    June    July    August    ...

Shipment      0           0        10        10     0       10      0
Picking       0           0        0         0      0       0       0
Destroy       ...

I’ve messed with the PIVIOT method, but I haven’t had much luck. At this point, all I have is the list of dates that I need between GETDATE() and GETDATE() - 12 months (retrieved with the query below):

DECLARE @BeginDate DATETIME
DECLARE @EndDate DATETIME

SET @BeginDate = GETDATE();
SET @EndDate = DATEADD(MONTH, -12, GETDATE());

WITH CTE_DatesTable
AS
(
    SELECT @EndDate AS [Date]

    UNION ALL

    SELECT  DATEADD(dd, 1, [date])
    FROM    CTE_DatesTable
    WHERE   DATEADD(dd, 1, [date]) <= @BeginDate
)

SELECT DISTINCT DATENAME(MONTH, [date]) + ' ' 
        + CAST(YEAR([date]) AS VARCHAR(4)) AS MonthYear,
                YEAR([date]) AS YearAsInt,
                MONTH([Date]) AS MonthAsInt
FROM            CTE_DatesTable
ORDER BY        YEAR([date]), MONTH([Date])
OPTION          (MAXRECURSION 0)

See the query in action here.

Is what I’m trying to accomplish possible? Am I going in the right direction? Any help would be appreciated!

  • 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-30T03:33:08+00:00Added an answer on May 30, 2026 at 3:33 am

    You can do this without pivot (the syntax for which I find daunting as well). Since you don’t know the actual layout of columns beforehand, I think this is easiest with dynamic SQL. Given the following table/sample data:

    USE tempdb;
    GO
    
    CREATE TABLE dbo.foo
    (
        ItemID INT, 
        MonthAsInt INT, 
        [Month] VARCHAR(12), 
        [Year] INT, 
        InvType VARCHAR(12), 
        Quantity INT
    );
    
    INSERT dbo.foo SELECT 4643,4 ,'April   ',2011,'Shipment',10
    UNION ALL SELECT 4643,5 ,'May     ',2011,'Shipment',10
    UNION ALL SELECT 4643,7 ,'July    ',2011,'Shipment',10
    UNION ALL SELECT 4643,8 ,'August  ',2011,'Destroy ',10
    UNION ALL SELECT 4643,11,'November',2011,'Shipment',25
    UNION ALL SELECT 4643,12,'December',2011,'Picking ',1;
    

    You can generate a list of months using a much simpler CTE, and build a dynamic SQL statement based off of that:

    DECLARE @sql NVARCHAR(MAX) = N'';
    
    ;WITH n AS 
    (
        SELECT TOP (12) d = DATEADD
        (
            MONTH, 
            -(ROW_NUMBER() OVER (ORDER BY [object_id]) - 1), 
            GETDATE()
        )
        FROM sys.objects
        ORDER BY d DESC
    )
    SELECT @sql = @sql + N',' + CHAR(13) + CHAR(10) + DATENAME(MONTH, d) 
        + ' = SUM(CASE WHEN [Year] = ' + CONVERT(VARCHAR(4), DATEPART(YEAR, d))
        + ' AND MonthAsInt = ' + CONVERT(VARCHAR(2), DATEPART(MONTH, d)) 
        + ' THEN Quantity ELSE 0 END)'
    FROM n
    ORDER BY d;
    
    SELECT @sql = N'SELECT InvType' + @sql + '
        FROM dbo.foo
        GROUP BY InvType';
    
    PRINT @sql;
    -- EXEC sp_executesql @sql;
    

    I put the PRINT there so you could test it out before running it. I wasn’t sure if you needed 12 months or 13 months, you can just change the TOP (12) to TOP (13) if you want 13 months, or remove the -1 if you don’t want the current month included.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.