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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:59:35+00:00 2026-06-15T08:59:35+00:00

I have a SQL Server database, I need to store an unknown number of

  • 0

I have a SQL Server database, I need to store an unknown number of columns, so I have 2 tables like this:

  1. Table Master, with these columns: id, data1, data2 (fixed columns)
  2. Table unknown_fields with these columns: id_master_record, id_field, value_field

I do the query like this:

SELECT id, data1, data2, 
  (SELECT value_field FROM unknown_fields
    WHERE id_master_record = Master.id AND id_field = 1) AS data3
  (SELECT value_field FROM unknown_fields 
    WHERE id_master_record = Master.id AND id_field = 2) AS data4
  (SELECT value_field FROM unknown_fields 
    WHERE id_master_record = Master.id AND id_field = 3) AS data5
 ... SO for each 
FROM Master

I want to know whether there is a better way to do this query.

  • 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-15T08:59:36+00:00Added an answer on June 15, 2026 at 8:59 am

    You could try this, it may be slightly faster. Best to test both. Clustering the second table on (id_master_record, id_field) will probably help both queries.

    Select
      m.id,
      m.data1,
      m.data2,
      Max(Case u.id_field When 1 Then u.value_field End) As data3,
      Max(Case u.id_field When 2 Then u.value_field End) As data4,
      Max(Case u.id_field When 3 Then u.value_field End) As data5
    From
      xmaster m
        Left Outer Join
      unknown_fields u
        On m.id = u.id_master_record
    Group By
      m.id,
      m.data1,
      m.data2;
    

    You could also try using Pivot

    Select
      id,
      data1,
      data2,
      [1] as data3,
      [2] as data4,
      [3] as data5
    From (
      Select
        m.id,
        m.data1,
        m.data2,
        id_field,
        value_field
      From
        Master m
          left outer join
        unknown_fields u
          On m.id = u.id_master_record
        ) as s
    Pivot (
      max(value_field)
    For 
        id_field In ([1], [2], [3])
    ) As p;
    

    http://sqlfiddle.com/#!6/f9bca/11

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

Sidebar

Related Questions

I have a SQL Server 2008 database with 2 tables. These tables are Address
i have stored the txt file to sql server database . i need to
I have a SQL Server 2005 database that has been deleted, and I need
I have data in an SQL Server 2005 database which I need to copy
I need to localize a SQL Server 2008 database. After investigating recommendations, I have
I have an existing SQL Server database, where I store data from large specific
I need to store a password has in a SQL server 2000 database. The
I currently have two joined tables in a SQL Server database, one with news
Alright, so I have a table that I need to store in a database.
I'm running SQL Server 2005 and .Net 2.0. I have some tables that need

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.