case when 的两种用法,查出结果横向,纵向展示

1.

select 
  county_id 
  ,county_name
  ,count(distinct user_id) as totalRow
  ,count(distinct user_id) as totalUser
  ,count(distinct case when result=120 then user_id end) as cgUser
  ,count(distinct case when result=101 then user_id end) as xrBj
  ,count(distinct case when result=134 then user_id end) as xrQx
  ,count(distinct case when result=111 then user_id end) as sXBj
  ,count(distinct case when result=103 then user_id end) as ksXh
  ,count(distinct case when result=130 then user_id end) as sqQx
from DW_XZZT_XR_GC_DT_20191114 group by county_id,county_name;

结果为:case when 的两种用法,查出结果横向,纵向展示

 

2   

 select case when result=120 then '携入成功'
             when result=103 then '携入申请-快速携回' end as name,sum(value) as value
from
(select result,count(distinct user_id) as value from DW_XZZT_XR_GC_DT_20191114 where 
result in (120,101,134,111,103,130) and incarrier='002' and result_up_time between '201811'and '201911' group by result)a group by name

case when 的两种用法,查出结果横向,纵向展示