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

  • Home
  • SEARCH
  • 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 7080111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:45:17+00:00 2026-05-28T06:45:17+00:00

This is a question maybe already asked. My query is SELECT Year, Month, Line,

  • 0

This is a question maybe already asked.

My query is

SELECT Year, Month, Line, SUM(value) as total FROM myTable

I’ve the following query result table:

Year  Month   Line     Total
-------------------------------------------
2011     2      B1     5203510.00
2011     3      B1     2228850.00
2011     4      B1     7258075.00
2011     5      B1     6305370.00
2011     6      B1     5540180.00
2011     7      B1     7624430.00
2011     8      B1     4042300.00
2011     9      B1     3308870.00
2011    10      B1     4983875.00
2011    11      B1     4636500.00
2011    12      B1     3987350.00
2012     1      B1      518400.00

I would like the following:

Year Line  Jan    Feb   Mar   Apr ..... December

2011 B1      0    52035  2228 725 ..... 3987350
2012 B1     51840 ... ... .... 

Please, can you help me how to translate query SQL from rows to columns?

  • 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-28T06:45:18+00:00Added an answer on May 28, 2026 at 6:45 am

    Essentially, you need to PIVOT your data. There are several examples on SO on how to do this. The tricky part is to convert the month number to a month name.

    This is accomplished in the example with DATENAME(month, DateAdd(month, [Month], 0)-1)

    SQL Statement

    SELECT  *
    FROM    ( 
              SELECT  Year, Line, Total, mnt = DATENAME(month, DateAdd(month, [Month], 0)-1)
              FROM    myTable
            ) mt          
    PIVOT   (MAX(Total) FOR [mnt] IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December])) AS PVT
    

    Test script

    ;WITH myTable AS (
      SELECT * FROM (VALUES
        (2011     , 2      , 'B1',      5203510.00)
        , (2011     , 3      , 'B1',      2228850.00)
        , (2011     , 4      , 'B1',      7258075.00)
        , (2011     , 5      , 'B1',      6305370.00)
        , (2011     , 6      , 'B1',      5540180.00)
        , (2011     , 7      , 'B1',      7624430.00)
        , (2011     , 8      , 'B1',      4042300.00)
        , (2011     , 9      , 'B1',      3308870.00)
        , (2011    , 10      , 'B1',      4983875.00)
        , (2011    , 11      , 'B1',      4636500.00)
        , (2011    , 12      , 'B1',      3987350.00)
        , (2012     , 1      , 'B1',       518400.00)
      ) AS myTable (Year, Month, Line, Total)
    )  
    SELECT  *
    FROM    ( 
              SELECT  Year, Line, Total, mnt = DATENAME(month, DateAdd(month, [Month], 0)-1)
              FROM    myTable
            ) mt          
    PIVOT   (MAX(Total) FOR [mnt] IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December])) AS PVT
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is (maybe) an already asked question... but i've some difficul to solve my
Maybe this question has been asked many times before, but I never found a
In this SO question, someone asked for calculating an angle from three points .
Sorry if this question was asked already. I started studying C# and noticed that
I already asked a similar question but this one is a bit different/specific: I'm
I have already asked this question in offical Flot google groups, but got no
I know this question has already been asked but I am in urgent need
i have already asked this question on asp.net forum, could not find any satisfactory
Dear all, the question like this one has been already asked , but among
This question maybe a better fit on The Business of Software forum but despite

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.