-1

以下は、データを取得するために必要なテーブルです。この例では、ここで使用する列のみを含めました。

保守可能

mainkey, account
-----------------
1, 100
2, 200
3, 300

チャイルドテーブル

linkedkeytomain, type, color
---------------------------
2, b, blue
2, y, yellow
2, r, red
2, g, green
2, w, white

私の目標は、1行のデータのみを表示するselectを記述できるようにすることです。ここで、Type='b'およびType='w'およびType='r'のみです。

例えば、

Account, c1, c2, c3
---------------------
200, blue, white, red

誰かがこの複雑な選択を取得する方法を教えてもらえますか?よろしくお願いします。

ブルモンド

4

1 に答える 1

1

あなたの問題は、グループをピボットする必要があるということです。簡単な方法は次のとおりです。

select account,
       max(case when seqnum = 1 then color end) as color1,
       max(case when seqnum = 2 then color end) as color2,
       max(case when seqnum = 3 then color end) as color3
from (select account, color,
             row_number() over (partition by account order by color) as seqnum
      from maintable mt join
           childtable ct
           on ct.linkedkeytomain = mt.mainkey
     ) t
group by account
于 2012-07-24T18:49:00.077 に答える