--case when--统计不同部门男女各有多少人select
dept_id,count(*) total,sum(case sex when'男'then1else0end) male,sum(case sex when'女'then1else0end) female
from
emp_sex
groupby
dept_id;
--行转列select
concat(constellation,",",blood_type) xzxx,##concat拼接函数
concat_ws("|", collect_list(name)) rentou
from
person_info
groupby
constellation,blood_type;
--列转行select
m.movie,
tbl.cate
from
movie_info m
lateral view
explode(split(category,",")) tbl as cate;
二、窗口函数
OVER():指定分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变而变化。
CURRENT ROW:当前行
n PRECEDING:往前n行数据
n FOLLOWING:往后n行数据
UNBOUNDED:起点,UNBOUNDED PRECEDING 表示从前面的起点, UNBOUNDED FOLLOWING表示到后面的终点
LAG(col,n,default_val):往前第n行数据
LEAD(col,n, default_val):往后第n行数据
NTILE(n):把有序窗口的行分发到指定数据的组中,各个组有编号,编号从1开始,对于每一行,NTILE返回此行所属的组的编号。注意:n必须为int类型。
--聚合select name,count(*)over()from business
where substring(orderdate,1,7)='2017-04'groupby name;
--各种聚合select name,orderdate,cost,sum(cost)over()as sample1,--所有行相加 sum(cost)over(partitionby name)as sample2,--按name分组,组内数据相加 sum(cost)over(partitionby name orderby orderdate)as sample3,--按name分组,组内数据累加 sum(cost)over(partitionby name orderby orderdate rowsbetweenUNBOUNDEDPRECEDINGandcurrentrow)as sample4 ,--和sample3一样,由起点到当前行的聚合 sum(cost)over(partitionby name orderby orderdate rowsbetween1PRECEDINGandcurrentrow)as sample5,--当前行和前面一行做聚合 sum(cost)over(partitionby name orderby orderdate rowsbetween1PRECEDINGAND1FOLLOWING)as sample6,--当前行和前边一行及后面一行 sum(cost)over(partitionby name orderby orderdate rowsbetweencurrentrowandUNBOUNDEDFOLLOWING)as sample7 --当前行及后面所有行 from business;
--结合其他函数使用select
name, orderdate, cost,
lag(orderdate,1)over(partitionby name orderby orderdate) last_order,
lead(orderdate,1)over(partitionby name orderby orderdate) next_order
from
business;
--ntileSELECT*FROM(select name,
orderdate,
cost,
ntile(5)over(orderby orderdate) n
from
business) t1
WHERE
n =1;
--percent_rankselect
name,
orderdate,
cost,
PERCENT_RANK()over(orderby orderdate) pr
from
business;
一、常用函数--nvl空字段赋值select comm, nvl(comm, -1) from emp;--case when--统计不同部门男女各有多少人select dept_id, count(*) total, sum(case sex when '男' then 1 else 0 end) male, sum(case sex when '女' then 1 else 0 end) femalefrom emp_sexgroup by