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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:36:20+00:00 2026-05-16T10:36:20+00:00

I have a Table1: ID Property 1 Name 2 City 3 Designation and Table2:

  • 0

I have a Table1:

ID  Property
1   Name
2   City
3   Designation

and Table2:

ID  RecordID  Table1ID  Value
1   1         1         David
2   1         2         Tokyo
3   2         1         Scott
4   2         3         Manager

The Table1ID of Table2 maps to Table1’s ID. Now I wish to show the Table1 Property column values as column headers and have a result set in format like:

RecordID     Name    City    Designation
1            David   Tokyo   NULL
2            Scott   NULL    Manager

What is the best/efficient way to achieve this in T-SQL considering that the number of records in Table1 (i.e. the columns in result set) can change and thus should be handled dynamically.

Although I tried PIVOT and CASE based queries, but have been struggling with both of them. 🙁

Any help/guidance would be appreciated.

Thanks!

Update:
I’ve been able to create the dynamic query, but one thing which I am still not able to understand is why MAX has been used in the CASE statements. Kindly ignore my noobness.

  • 1 1 Answer
  • 3 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-05-16T10:36:21+00:00Added an answer on May 16, 2026 at 10:36 am

    Use:

      SELECT t2.recordid,
             MAX(CASE WHEN t1.property = 'Name' THEN t2.value END) AS name,
             MAX(CASE WHEN t1.property = 'City' THEN t2.value END) AS city,
             MAX(CASE WHEN t1.property = 'Designation' THEN t2.value END) AS designation
        FROM TABLE2 t2
        JOIN TABLE1 t1 ON t1.id = t2.table1id
    GROUP BY t2.recordid
    ORDER BY t2.recordid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have bean like: <bean id=test> ........... <property name=myQuery value=select a,b,c from table1 where
i have two different tables table1 - property ================= id,name, address, city state, zip
I have Table1 and Table2 related on Table1.ID. There can be zero or more
So lets say I have TABLE1 and TABLE2. CREATE TABLE TABLE1 ( first_id int
I have a geo collection that contains items like: [state name] [city], [state] [country]
I have two tables table COMMUNITY id, name, etc. table PROPERTY id, community_id, type,
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field
I have a table which contains house details called property. I am creating a
I have given a table background image using css background-image property. The cells are
I have a Data Class called MyTable, and the source property is TABLE (table

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.