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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:19:57+00:00 2026-06-06T05:19:57+00:00

I have written my pivot SQL and it is working. The current SQL Statement

  • 0

I have written my pivot SQL and it is working. The current SQL Statement is below and I will show an example of how the data is returned. I need to add one column and this is where it bombs:

Select 
    ProductGroup,
    Origin,
    Destination,
    [YEAR], 
    Isnull([Jan],0) as "Jan",
    isnull([Feb],0) as "Feb",
    isnull([Mar],0) as "Mar",
    isnull([Apr],0) as "Apr",
    isnull([May],0) as "May",
    isnull([Jun],0) as "Jun",
    isnull([Jul],0) as "Jul",
    isnull([Aug],0) as "Aug",
    isnull([Sep],0) as "Sep",
    isnull([Oct],0) as "Oct",
    isnull([Nov],0) as "Nov",
    isnull([Dec],0) as "Dec"
FROM
(
SELECT 
    p.ProductGroup, 
    S.Origin, 
    S.FinalDest AS Destination,
    SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3) as MonthAbreviated, 
    Year(BolDate) AS [Year], 
    Count(*) AS [Total]
--  ,COUNT(S.Purchase#) AS [TheTotal]
    FROM 
        dbo.Contracts c INNER JOIN dbo.Purchases pu ON c.[Contract#] = pu.[Contract#]
        INNER JOIN dbo.Products as p ON pu.Product = p.Product 
        INNER JOIN dbo.Shipments S ON pu.[Purchase#] = S.[Purchase#]
WHERE 
    Year(BolDate)<>1994 AND
    pu.Cancelled=0 AND S.[Status]='L'
GROUP BY p.ProductGroup, S.Origin, S.FinalDest,Year(BolDate), SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3)

) AS SourceTable
PIVOT
(
sum( Total )
FOR MonthAbreviated IN ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
) AS PivotTable

EXAMPLE of results:

ProductGroup   Origin  Destination  YEAR        Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 
Nail           Bath    Toronto      2012        0   0   0   10  1   0   0   0   0   0   0   0 
Nail           Cedars  Toronto      2011        0   0   0   0   0   0   0   0   0   0   25  53

I need to add a column that displays the total of the row. For example row 1 is 11 and row 2 is 78. In my select, I thought simply adding “Total” to the query would do it but I get an invalid column each time.

In other words I get this error:

Msg 207, Level 16, State 1, Line 6 Invalid column name ‘Total’.

Select 
    ProductGroup,
    Origin,
    Destination,
    [YEAR], 
    [Total],
    Isnull([Jan],0) as "Jan",
    isnull([Feb],0) as "Feb",
    isnull([Mar],0) as "Mar",
    isnull([Apr],0) as "Apr",
    isnull([May],0) as "May",
    isnull([Jun],0) as "Jun",
    isnull([Jul],0) as "Jul",
    isnull([Aug],0) as "Aug",
    isnull([Sep],0) as "Sep",
    isnull([Oct],0) as "Oct",
    isnull([Nov],0) as "Nov",
    isnull([Dec],0) as "Dec"
FROM
(
SELECT 
    p.ProductGroup, 
    S.Origin, 
    S.FinalDest AS Destination,
    SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3) as MonthAbreviated, 
    Year(BolDate) AS [Year], 
    Count(*) AS [Total]
--  ,COUNT(S.Purchase#) AS [TheTotal]
    FROM 
        dbo.Contracts c INNER JOIN dbo.Purchases pu ON c.[Contract#] = pu.[Contract#]
        INNER JOIN dbo.Products as p ON pu.Product = p.Product 
        INNER JOIN dbo.Shipments S ON pu.[Purchase#] = S.[Purchase#]
WHERE 
    Year(BolDate)<>1994 AND
    pu.Cancelled=0 AND S.[Status]='L'
GROUP BY p.ProductGroup, S.Origin, S.FinalDest,Year(BolDate), SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3)

) AS SourceTable
PIVOT
(
sum( Total )
FOR MonthAbreviated IN ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
) AS PivotTable
  • 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-06T05:19:59+00:00Added an answer on June 6, 2026 at 5:19 am

    If you want the Total for the row, you can simply add the columns together to get the total. So you will want to add the following line:

    ([Jan] + [Feb] + [Mar] + [Apr] + [May] + [Jun] + [Jul] + [Aug] + [Sep] + [Oct] + [Nov] + [Dec]) as Total
    

    making your query:

    Select 
        ProductGroup,
        Origin,
        Destination,
        [YEAR], 
        Isnull([Jan],0) as "Jan",
        isnull([Feb],0) as "Feb",
        isnull([Mar],0) as "Mar",
        isnull([Apr],0) as "Apr",
        isnull([May],0) as "May",
        isnull([Jun],0) as "Jun",
        isnull([Jul],0) as "Jul",
        isnull([Aug],0) as "Aug",
        isnull([Sep],0) as "Sep",
        isnull([Oct],0) as "Oct",
        isnull([Nov],0) as "Nov",
        isnull([Dec],0) as "Dec",
        ([Jan] + [Feb] + [Mar] + [Apr] + [May] + [Jun] + [Jul] + [Aug] + [Sep] + [Oct] + [Nov] + [Dec]) as Total
    FROM
    (
    SELECT 
        p.ProductGroup, 
        S.Origin, 
        S.FinalDest AS Destination,
        SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3) as MonthAbreviated, 
        Year(BolDate) AS [Year], 
        Count(*) AS [Total]
    --  ,COUNT(S.Purchase#) AS [TheTotal]
        FROM 
            dbo.Contracts c INNER JOIN dbo.Purchases pu ON c.[Contract#] = pu.[Contract#]
            INNER JOIN dbo.Products as p ON pu.Product = p.Product 
            INNER JOIN dbo.Shipments S ON pu.[Purchase#] = S.[Purchase#]
    WHERE 
        Year(BolDate)<>1994 AND
        pu.Cancelled=0 AND S.[Status]='L'
    GROUP BY p.ProductGroup, S.Origin, S.FinalDest,Year(BolDate), SUBSTRING('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec', (Month(boldate) * 4) - 3, 3)
    
    ) AS SourceTable
    PIVOT
    (
    sum( Total )
    FOR MonthAbreviated IN ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
    ) AS PivotTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a code to add user into DB. I need to redirect
have written a stochastic simulation in Java, which loads data from a few CSV
I have written a code for store the current date & time value. It
I have written below code in htaccess RewriteRule ^([a-zA-Z_-]+).php$ http://www.domain.com/myfile.php?page=inc- $1.php [NC] I don't
I have written an AppleScript which when supplied with a Windows network link, will
I have written following regex But its not working. Can you please help me?
I have written an application: parser and web portal. They are both using SQL
Have written a Binary Search Tree that stores data on ships, the key for
I have written a function to convert string to integer if ( data !=
I have written a small HTTP server and everything is working fine locally, but

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.