PostgreSQL的case when

select tt.dict_label as display_item,t1.ratio from
(select t.product_level,round(sum(case when t.detect_result='2' then 1 else 0 end)::NUMERIC/count(1)::NUMERIC,4) as ratio
from t_detect_record t
where t.district='3' and t.e_commerce_platform='3'
group by t.product_level) t1
left join t_dict tt on t1.product_level=tt.dict_value
where tt.dict_type='product_level'


select country, sum( case when sex = ‘1’ then   population else 0 end),  男性人口

 sum( case when sex = ‘2’ then  population else 0 end)   女性人口

from table_a   group by country;

 
    原文作者:PostgreSQL
    原文地址: https://www.cnblogs.com/esther-qing/p/5908955.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞