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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:43:03+00:00 2026-06-14T16:43:03+00:00

Could anybody guide me how to pivot my data. I have: RowID Dimension Value

  • 0

Could anybody guide me how to pivot my data. I have:

RowID Dimension Value
1 Country Italy
1 Year 2011
1 GDP 4
1 Population 6
2 Country Spain
2 Year 2011
2 GDP 7
2 Population 5

I want in a such way:

RowID Country Year GDP Population
1 Italy 2011 4 6
2 Spain 2011 7 5

P.S. I use MS SQL Server 2008 R2 Express Edition. I tried to use PIVOT but it returned many rows with NULL so I could not figure out.

  • 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-14T16:43:04+00:00Added an answer on June 14, 2026 at 4:43 pm

    You can use PIVOT for this. This can be hard-coded if you know all of the values:

    select *
    from
    (
      select rowid, dimension, value
      from yourtable
    ) src
    pivot
    (
      max(value)
      for dimension in ([Country], [Year], [GDP], [Population])
    ) piv
    

    See SQL Fiddle with Demo

    Or of you do not have access to the PIVOT function, then you can use an aggregate with a CASE:

    select rowid,
      max(case when dimension = 'country' then value end) country,
      max(case when dimension = 'Year' then value end) Year,
      max(case when dimension = 'GDP' then value end) GDP,
      max(case when dimension = 'Population' then value end) Population
    from yourtable
    group by rowid
    

    See SQL Fiddle with Demo

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

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Dimension) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT rowid, ' + @cols + ' from 
                 (
                    select rowid, dimension, value
                    from yourtable
                ) x
                pivot 
                (
                    max(value)
                    for dimension in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    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

Could anybody please tell me which is best guide/book/material for socket programming in C?
Could anybody be so kind to give me a simple example of reification in
Could anybody explain to me the process of uploading to and downloading form and
Could anybody give an example to demonstrate the usage of the Invariant Culture ?
Could anybody brief about user_token functionality in Auth module? What is a use and
Could anybody explain, why next code isn't compiling? I'm creating a partially-specialized Map and
there is a list of ruby gems for twitter could anybody recommend me one
I am getting below exception in runtime, could anybody suggest what is wrong? Microsoft
I was wondering if anybody could help me with a method of creating a
I am new to php and was wondering if anybody could help me with

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.