2017-03-02 3 views
1

特定の表では、次のような特定の列を表示するようにクエリしています。列として表示するデータの削除

select an.animals_key,   
     an.anim_name, 
     an.sex, 
     an.date_of_birth, 
     tt.trait_key,    
     --case when tt.trait_key = 152 then 'Left Eye Pigment' 
      --when tt.trait_key = 153 then 'Right Eye Pigment' end as trait_key, 
     tt.trait_value, 
     tt.observation_date 

from animals an 
join traits tt on tt.soc_code = an.soc_code and tt.animals_key = an.animals_key and tt.trait_key in (152,153) 
where an.soc_code = 'AUHF' limit 10 

このクエリは私に次のデータを生成しました。

enter image description here

列の数が常に事前に定義されている場合は、私はそれらの列

+0

特性値はまったく異なりますか?それは100のすべての方法のように見えます。 – peterm

答えて

1

内のすべての形質値を置くことができるように、私は1つの列に152一つとしての列と153を置くことができる方法はあります(あなたの場合は152,153)条件付き集計を行うことができます

select an.animals_key, an.anim_name, an.sex, an.date_of_birth, tt.observation_date 
     min(case when tt.trait_key = 152 then tt.trait_value end) "152", 
     min(case when tt.trait_key = 153 then tt.trait_value end) "153" 
    from animals an join traits tt 
    on tt.soc_code = an.soc_code 
    and tt.animals_key = an.animals_key 
    and tt.trait_key in (152, 153) 
where an.soc_code = 'AUHF' 
group by an.animals_key, an.anim_name, an.sex, an.date_of_birth, tt.observation_date 
関連する問題