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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:41:18+00:00 2026-06-15T17:41:18+00:00

I have two tables x and y Table x has 24 columns each of

  • 0

I have two tables x and y

Table x has 24 columns each of which holds a different Id value

Table Y has an Id value column and a description

I need to write a procedure, query or view that returns the descriptions for each of the 24 Id values contained in table x in the best performing way possible.

I’ve written a view that calls a function 24 times. The function returns the description based on the Id provided.
Whilst this works it doesn’t perform particularly well.

Is there a technique I should use where this number of descriptions are required from a single table?

Here is the definition for table x (with non relevant columns removed for clarity)

    [DefinitiveHLATypeId] [int] IDENTITY(1,1) NOT NULL,
[PersonId] [int] NOT NULL,
[A_X] [int] NULL,
[A_Y] [int] NULL,
[B_X] [int] NULL,
[B_Y] [int] NULL,
[Bw_X] [int] NULL,
[Bw_Y] [int] NULL,
[C_X] [int] NULL,
[DRB1_X] [int] NULL,
[DRB1_Y] [int] NULL,
[DRB3_X] [int] NULL,
[DRB3_Y] [int] NULL,
[DRB4_X] [int] NULL,
[DRB4_Y] [int] NULL,
[DRB5_X] [int] NULL,
[DRB5_Y] [int] NULL,
[DQA_X] [int] NULL,
[DQA_Y] [int] NULL,
[DQB_X] [int] NULL,
[DQB_Y] [int] NULL,
[DPA1_X] [int] NULL,
[DPA1_Y] [int] NULL,
[DPB1_X] [int] NULL,
[DPB1_Y] [int] NULL

Here is the definition for table y (with non relevant columns removed for clarity)

    [AntigenId] [int] IDENTITY(1,1) NOT NULL,
[AntigenDescription] [varchar](2000) NOT NULL

The relationship between the two tables is between the _X & _Y columns in table x and the AntigenId column in table Y

I need to return the Antigen Description for each of the _X & _Y columns in table x.

  • 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-15T17:41:19+00:00Added an answer on June 15, 2026 at 5:41 pm

    Without seeing any sample data in your tables, if you have a table that have 24 columns of data that you need to compare to another table there are a few ways to do this.

    First, you could perform multiple joins on the table for each column similar to this:

    select *
    from tablex x
    left join tabley y1
      on x.col1 = y1.id
    left join tabley y2
      on x.col2 = y2.id --- add more joins
    

    I don’t know if joining the tables 24 times would be the most efficient, so it might be easier to perform an UNPIVOT on the table with 24 columns and join on that result:

    select x.value,
      y.description
    from 
    (
      select value, col
      from tablex
      unpivot
      (
        value
        for col in (col1, col2, col3, col4, col5)
      ) unpiv
    ) x
    left join tabley y
      on x.value = y.id
    

    See SQL Fiddle with Demo

    The UNPIVOT is the same as using a UNION ALL on tablex to transform the data from multiple columns into rows of data which will make it easier to join on:

    select *
    from
    (
      select col1 value, 'col1' col
      from tablex
      union all
      select col2 value, 'col2' col
      from tablex
      union all
      select col3 value, 'col3' col
      from tablex
      union all
      select col4 value, 'col4' col
      from tablex
      union all
      select col5 value, 'col5' col
      from tablex
    ) x
    left join tabley y
      on x.value = y.id
    

    Based on your edit, your query would be similar to this:

    select x.value,
      y.AntigenDescription
    from 
    (
      select DefinitiveHLATypeId, PersonId, value, col
      from tablex
      unpivot
      (
        value
        for col in (A_X, A_Y, B_X, B_Y, Bw_X, Bw_Y,
                        C_X, DRB1_X, DRB1_Y, DRB3_X, DRB3_Y,
                        DRB4_X, DRB4_Y, DRB5_X, DRB5_Y, DQA_X, DQA_Y,
                        DQB_X, DQB_Y, DPA1_X, DPA1_Y,
                        DPB1_X, DPB1_Y
      ) unpiv
    ) x
    left join tabley y
      on x.value = y.AntigenId
    

    Note: In the long term my suggestion would be to redesign the tablex because having a table structured in this way will make it difficult to access the data.

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

Sidebar

Related Questions

I have two tables. The first table has a column with a lot of
I have two tables, each one has a primary ID column as key. I
I have two tables. Widgets, which has information about each widget (Color, size, etc);
I have two tables. The first table holds simple user data and has the
Let's say I have two tables: Table 1 has the columns NOTE_ID (a unique
I have two tables - table1 and table2 table1 has 14 columns and table2
I have two tables: table1, table2. Table1 has 10 columns, table2 has 2 columns.
I have two tables; Leads, Territories and Referrers. Lead has columns: ID, Name, TerritoryId
I have two tables: a 'userlist' that has a 'grade' and 'userID' columns, and
I have created a table in Visual Studio. That table has two columns. First

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.