首页 > 解决方案 > SQL 如何修改这张表

问题描述

如何在sql中获取此表:

id   cunsumption_bhd cunsumption_metha 
id1  21.0            10.2

从此表中:

id   type   cunsumption
id1  bhd    21.0
id1  metha  10.2

标签: sql

解决方案


一种方法是条件聚合:

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;

推荐阅读