1

私は2つのテーブルxとyを持っています

テーブル x には 24 の列があり、それぞれが異なる Id 値を保持しています

テーブル Y には ID 値列と説明があります

テーブル x に含まれる 24 個の Id 値のそれぞれの説明を可能な限り最適な方法で返すプロシージャ、クエリ、またはビューを作成する必要があります。

関数を 24 回呼び出すビューを作成しました。この関数は、提供された Id に基づいて説明を返します。これは機能しますが、特にうまく機能しません。

1 つのテーブルからこの数の説明が必要な場合に使用すべき手法はありますか?

テーブル x の定義は次のとおりです (明確にするために関連しない列は削除されています)。

    [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

テーブル y の定義は次のとおりです (わかりやすくするために、関係のない列は削除されています)。

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

2 つのテーブル間の関係は、テーブル x の _X & _Y 列とテーブル Y の AntigenId 列の間にあります。

テーブル x の _X 列と _Y 列のそれぞれについて、Antigen の説明を返す必要があります。

4

2 に答える 2

0

参加する

select tbl.col1ID, desc1.desc, tbl.col2ID, desc2.desc
from tbl 
join desc as desc1 
  on desc1.ID = col1ID 
join desc as desc2 
  on desc2.ID = col1ID 
于 2012-12-10T14:20:45.053 に答える
0

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.

于 2012-12-10T14:29:53.980 に答える