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;

 
posted @ 2016-09-26 13:55  esther-qing  阅读(17295)  评论(0编辑  收藏  举报