I'm supposing here that feature_id, experiment_id
is unique key for Feature_values
.
Standard SQL way to do this is to make n joins
select
F.feature_name,
FV1.value as exp1,
FV2.value as exp2,
FV3.value as exp3
from Features as F
left outer join Feature_values as FV1 on FV1.feature_id = F.feature_id and FV1.experiment_id = 1
left outer join Feature_values as FV2 on FV2.feature_id = F.feature_id and FV2.experiment_id = 2
left outer join Feature_values as FV3 on FV3.feature_id = F.feature_id and FV3.experiment_id = 3
Or pivot data like this (aggregate max
is not actually aggregating anything):
select
F.feature_name,
max(case when E.experiment_name = 'exp1' then FV.value end) as exp1,
max(case when E.experiment_name = 'exp2' then FV.value end) as exp2,
max(case when E.experiment_name = 'exp3' then FV.value end) as exp3
from Features as F
left outer join Feature_values as FV on FV.feature_id = F.feature_id
left outer join Experiments as E on E.experiment_id = FV.experiment_id
group by F.feature_name
order by F.feature_name
you can also consider using json (in 9.3 version) or hstore to get all experiment values into one column.