建设电影网站点击播放是乱页的,苏州seo网络优化公司,网站开发 .net 开源,自己怎么设计公园一、条件函数
if 条件函数 if函数是最常用到的条件函数#xff0c;其写法是if(xn,a,b), xn代表判断条件#xff0c;如果xn时#xff0c;那么结果返回a ,否则返回b。 selectif(age 25 or age is null, 25岁以下, 25岁以上) as age_cnt,count(1) as number
from table…一、条件函数
if 条件函数 if函数是最常用到的条件函数其写法是if(xn,a,b), xn代表判断条件如果xn时那么结果返回a ,否则返回b。 selectif(age 25 or age is null, 25岁以下, 25岁以上) as age_cnt,count(1) as number
from table1
group by age_cnt;
case when case when 与if的作用基本相同也是按照条件更换列中的内容区别是case when 可以对 多个条件进行转换需要注意的是结尾需要加end作为结束标志 case 测试表达式
when 简单表达式1 then 结果表达式1
when 简单表达式2 then 结果表达式2
.......when 健达表达式n then 结果表达式n
[else 结果表达式 n1]
end--举例
select case when age 25 or age is null then 25岁以下else 25岁及以上end as age_cnt,
count(1) as number
from table1
group by age_cnt;-- 举例
select device_id,gender,case when age20 then 20岁以下when age20 and age24 then 20-24岁when age25 then 25岁及以上else 其他end as age_cut
from table1;
二、运用案例
2.1 行转列
问题描述 数据准备 create table if not exists test(col1 string comment ,col2 string comment ,col3 string comment ) comment 测试表;insert overwrite table testvalues (a,g,11),(a,f,23),(a,d,9),(b,g,5),(b,f,8),(b,d,47);
数据分析
利用case when 进行行转列
selectcol1,case col2 when g then col3 else 0 end as g,case col2 when f then col3 else 0 end as f,case col2 when d then col3 else 0 end as d
from test; 最后分组求max值即可
selectcol1,max(case col2 when g then col3 else 0 end) as g,max(case col2 when f then col3 else 0 end) as f,max(case col2 when d then col3 else 0 end) as d
from test
group by col1; 最终的输出结果 小结