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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:47:29+00:00 2026-06-17T08:47:29+00:00

Given this data: Name Property Value ———- ———- ———- Bob Hair Red Bob Eyes

  • 0

Given this data:

Name       Property   Value
---------- ---------- ----------
Bob        Hair       Red       
Bob        Eyes       Blue      
Fred       Hair       Brown     
Fred       Height     Tall      

what SQL would be required to produce these results?

Property   Bob        Fred
---------- ---------- ----------
Hair       Red        Brown     
Eyes       Blue             
Height                Tall     

I’m using SQL Server 2008, but a generic solution would be nice.

  • 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-17T08:47:30+00:00Added an answer on June 17, 2026 at 8:47 am

    You did not specify what RDBMS you are using but this is a pivot. You can use an aggregate function and a CASE expression in all databases:

    select property,
      max(case when name='Bob' then value else '' end) Bob,
      max(case when name='Fred' then value else '' end) Fred
    from yourtable
    group by property
    

    See SQL Fiddle with Demo

    If you are using a database that has a PIVOT function (SQL Server 2005+/Oracle 11g+), then your code will be similar to this:

    select *
    from
    (
      select property, name, value
      from yourtable
    ) src
    pivot
    (
      max(value)
      for name in (Bob, Fred)
    ) piv
    

    See SQL Fiddle with Demo

    The above queries work great, if you know the name values ahead of time, but if you don’t then you will want to use dynamic sql:

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

    See SQL Fiddle with Demo

    All three will produce the same result:

    | PROPERTY |  BOB |  FRED |
    ---------------------------
    |     Eyes | Blue |       |
    |     Hair |  Red | Brown |
    |   Height |      |  Tall |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given this coffeescript data = Foo: B Foos: [ A, B ] Blah: Id:
Given this example data set: ----------------------------- | item | date | val | -----------------------------
Given data like this C1<-c(3,-999.000,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,4,3,6,-999.000) DF<-data.frame(ID=c(A,B,C,D,E),C1=C1,C2=C2,C3=C3) How do I go about removing
Given a data set like this; +-----+---------------------+--------+ | id | date | result |
I get this error TypeError at /debate/1/ get_context_data() takes exactly 2 arguments (1 given)
when I look up the syntax for .data(), it gives examples like this: $('body').data('foo',
This example uses a StringWriter to hold the serialized data, then calling ToString() gives
I need query to get data between two days.I tried with this.But it gives
Given this method to work on a HTML page in a webbrowser: bool semaphoreForDocCompletedEvent;
I am trying to set a value to a Nested Property of Class dynamically

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.