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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:46:40+00:00 2026-06-08T08:46:40+00:00

I have a Person table and an attribute table. An Person can have 0

  • 0

I have a Person table and an attribute table. An Person can have 0 or more attributes, so I have this relationship stored in an intermediary key-value table, eg

------------         --------------       ---------------
|Person     |        |key val tbl |       | attribute   |
 -----------         --------------       ---------------
|p_id|p_name|        | p_id |a_id |       | a_id |a_name|
| 1  |simon |        |  1   | 1   |       |  1   | tall |
| 2  |eric  |        |  1   | 2   |       |  2   | cool |
                     |  2   | 2   |

How could I write a query to return something like this?

|name |attr1|attr2|  
|simon|tall |cool |
|eric |     |cool |

There are only ~6 attributes and that will not likely change.

Thanks!

  • 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-08T08:46:43+00:00Added an answer on June 8, 2026 at 8:46 am

    You need to join and aggregate:

    select p.name,
           max(case when a.a_id = 1 then a.a_name end) as attr1,
           max(case when a.a_id = 2 then a.a_name end) as attr2
    from person p join
         keyval kv
         on p.key = kv.key join
         attribute a
         on kv.a_id = a.a_id
    group by p.name
    

    This query joins the three tables together, which will produce a table with one row for each person/attribute pair. The final group by aggregates at the person level, “pivoting” the attributes across the row. The max(case …) expression simply chooses the value of attribute for column .

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

Sidebar

Related Questions

I have three tables like this: Person table: person_id | name | dob --------------------------------
I have this person table as super parent, id firstname lastname email telephone ...
Suppose I have a Person table that looks like this, where fk_FriendID is related
Suppose I have table Person table Employee, which inherits Person. I want to get
Say I have a Person table with 200000 records, there's a clustered index on
Say I have a person table in a database. Whenever a new person is
I have a table Person which contains 2 fields.In my another database i have
imaging i have 3 tables, person, boy, girl table person{ id ...} table boy{
I have a table called Person which I have already mapped in hibernate I
I have table with date/time column and person column. Each person has multiple records

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.