SQL基础

CASS WHEN.....THEN 基础使用~!

select e.chairdevice_id,SUM(IFNULL(e.d,0))/SUM(IFNULL(e.c,0)) from (select b.chairdevice_id,CASE when contents_type=1 then b.a END c,CASE when contents_type=2 then b.a END d from (select  chairdevice_id,contents_type,count(contents_type) a from n_tcpdatas_2_061102 where contents like '%50%' and contents_type='2' group by chairdevice_id
union
select  chairdevice_id,contents_type,count(contents_type) a from n_tcpdatas_2_061102 where contents like '%50%' and contents_type='1' group by chairdevice_id) b) e GROUP BY e.chairdevice_id

 

你可能感兴趣的:(SQL基础)