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

The Archive Base Latest Questions

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

I have the classical person -> person attributes scheme. So, like this: person(PK) <-

  • 0

I have the classical person -> person attributes scheme.
So, like this: person(PK) <- person_attribute(FK)

What I need is a query to get one row where a person is joined with her attributes.
For example, to transform:

Person:
{ ID = 123456, Name = 'John Smith', Age = 25 }
Attributes:
1) { PersonID = 123456, AttributeTypeID = 'Height', AttributeValue = '6'6'''}
2) { PersonID = 123456, AttributeTypeID = 'Weight', AttributeValue = '220lbs'}
3) { PersonID = 123456, AttributeTypeID = 'EyeColor', AttributeValue = 'Blue'}

To:

PersonWithAttributes
{
 ID = 123456, Name = 'John Smith', Age = 25, Height = '6'6''', Weight = '220lbs', EyeColor = 'Blue'
}

To make things worse my persons are in a table variable.

So, I have (in a sp with a parameter of person_id):

--result table
declare @people_info table
(
person_id int,
name nvarchar(max),
age int,
height nvarchar(10) null,
weight nvarchar(10) null,
eye_color nvarchar(16) null
)


insert into @people_info
 select person_id, name, age, null, null, null
  from dbo.HR.people where person_id = @person_id

update pi
 set
  pi.height = (select pa.attribute_value where pa.attribute_type_id = 'Height'),
  pi.height = (select pa.attribute_value where pa.attribute_type_id = 'Weight'),
  pi.eye_color = (select pa.attribute_value where pa.attribute_type_id = 'EyeColor')
 from
  @people_info pi
   inner join dbo.HR.person_attributes pa on pi.person_id = pa.person_id

select * from @people_info

Which of course does not work for some reason.
If I query the two joined tables and select “pa.attribute_value where pa.attribute_type_id = ‘someval'” I get the correct value. But the update does not work.

Of course, I can write this as three updates, but I am thinking that it will be faster to do one join and then to filter in the update clause.

Also, please keep in mind that my attributes are spread over three tables, not just the attributes table. So, this is why I have the table variable.

Any help is very welcome. Maybe I am going about this the wrong way. Performance matters. What is the most performant way to accomplish this?

Thank you very much.

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

    Try this code for update with pivot:

        update 
            pi
        set
            pi.height = pa.Height
            pi.weight = pa.Weight
            pi.eye_color = pa.EyeColor
        from
            @people_info pi
        inner join 
            (
                SELECT
                     person_id
                    ,[Height] Height
                    ,[Weight] Weight
                    ,[EyeColor] EyeColor
                FROM
                (
                    SELECT
                          attribute_type_id
                        , attribute_value
                        , person_id   
                    FROM
                        dbo.HR.person_attributes pa
                ) pa
                PIVOT
                (
                    MAX(attribute_value) FOR attribute_type_id IN ([Height],[Weight],[EyeColor])
                )pvt
    
            ) pa
        on 
            pi.person_id = pa.person_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an arrays that looks like this: Array ( [0] => stdClass Object
I have classical python server. I made a CGI script. I would like from
I have someone I need to train in Java. That person is well versed
I have the classical Symphony flash message implementation: $_SESSION['flash'] = This is a flash
A classical relational database would have a field like USER_ID which would typically be
I wonder how many of you have implemented one of computer science's classical algorithms
One of the classical reasons we have a database deadlock is when two transactions
we have a classical 3-tier-architecture application. Now we face a little problem and we
Classical question at tests: I have 4 dimensions: Markets: Sparse, 10 stored members, 10
I have to do a user registration form (classical layout with two columns, labels

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.