2017-03-02 6 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

방법이 열의 개수는 항상 미리 경우 I 그 열에

+0

형질 값이 전혀 다른가요? 그것은 마치'100'처럼 보입니다. – peterm

답변

1

모든 특성 값을 넣을 수 있도록 전 (하나 개의 컬럼에 하나 개의 컬럼 (153)으로 (152)를 넣을 수 있는가 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