如何在sql中获取此表:
id cunsumption_bhd cunsumption_metha
id1 21.0 10.2
从此表:
id type cunsumption
id1 bhd 21.0
id1 metha 10.2
答案 0 :(得分:0)
对表的两个“实例”都使用带有类型过滤器的自联接。
select t1.id,
t1.consumption as consumption_bhd,
t2.consumption as consumption_metha
from the_table t1
join the_table t2 on t1.id = t2.id and t2.type = 'metha'
where t1.type = 'bhd'
and t1.id = 'id1';
答案 1 :(得分:0)
一种方法是条件聚合:
select max(case when type = 'bhd' then consumption end) as consumption_bhd,
max(case when type = 'metha' then consumption end) as consumption_metha
from t;