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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:49:26+00:00 2026-06-13T10:49:26+00:00

I have a table in the following format Country_Code | 1960 | 1961 |

  • 0

I have a table in the following format

Country_Code | 1960 | 1961 | 1962 | ..... | 2011
------------------------------------------------
IND          | va11 | va12 | va13 | ..... | va1x
AUS          | va21 | va22 | va23 | ..... | va2x
ENG          | va31 | va32 | va33 | ..... | va3x

I want to convert it into the below format

Country_Code | Year | Value
---------------------------
IND          | 1960 | va11
IND          | 1961 | va12
IND          | 1962 | va13
.
.
IND          | 2011 | va1x
AUS          | 1960 | va21
AUS          | 1961 | va22
AUS          | 1962 | va23
.
.
AUS          | 2011 | va2x
ENG          | 1960 | va31
ENG          | 1961 | va32
ENG          | 1962 | va33
.
.
ENG          | 2011 | va3x

How can this be achieved by sql query or sql server assembly ?

  • 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-13T10:49:28+00:00Added an answer on June 13, 2026 at 10:49 am

    You can be done with UNPIVOT. If you have a known number of columns then you can hard-code the values:

    select Country_Code, year, value
    from yourtable
    unpivot
    (
      value 
      for year in ([1960], [1961], [1962], [2011])
    ) u
    

    See SQL Fiddle with Demo

    If you have an unknown number of columns, then you can use dynamic sql:

    DECLARE @colsUnpivot AS NVARCHAR(MAX),
       @query  AS NVARCHAR(MAX)
    
    select @colsUnpivot = stuff((select ','+quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('yourtable') and
                   C.name != 'Country_Code'
             for xml path('')), 1, 1, '')
    
    set @query 
      = 'select country_code, year, value
         from yourtable
         unpivot
         (
            value
            for year in ('+ @colsunpivot +')
         ) u'
    
    exec(@query)
    

    See SQL Fiddle with Demo

    Of you can even use a UNION ALL:

    select country_code, '1960' year, 1960 value
    from yourtable
    union all
    select country_code, '1961' year, 1961 value
    from yourtable
    union all
    select country_code, '1962' year, 1962 value
    from yourtable
    union all
    select country_code, '2011' year, 2011 value
    from yourtable
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I have a table with a following format. PID ID Label Value ------------------------------------------ 1
I have a table (Order_line_t) with the following format: Order_ID Product_ID Quantity -------- ----------
I have a php query the returns the following JSON format from a table.
I have a php query the returns the following JSON format from a table.
After doing some joins and stuff I have table in the following format name
I have a table named People in the following format: Date | Name .
I have data in a table in following format ID DocNumber RevOrder 1 DOC-001
So I have a table which is structured in the following format: in_click first_name
I have a table in the following format: Item A | Item B |
I have a table with the following format. mysql> describe unit_characteristics; +----------------------+------------------+------+-----+---------+----------------+ | Field

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.