将数据显示为列
问题描述:
在给定的表格中;我正在查询以显示某些列如下将数据显示为列
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
此查询生成了以下数据。
有什么办法,我可以把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
特征值是否有所不同?它看起来像是100。 – peterm