问题:有20行数值,根据时间排序,如果active-tag大于15则排序为空,满足按正序排序
思路:1、找到满足要求的表进行正序排序
2、找到不满足要求的表进行排序,如果不为空,就返回null,
3、两个表相互union,进行合并。
数值:
00:00,1,10.00
01:00,1,
02:00,1,20.00
03:00,1,
04:00,1,
05:00,1,70.00
--自定义UDF函数,要记得继承UDF函数
package com.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
import java.util.ArrayList;
public class Range extends UDF {
public ArrayList<Integer> evaluate(Integer startNum, Integer endNum) {
if (startNum == null || endNum == null || startNum > endNum) {
return null;
}
// 对数字进行list集合 [1,2] [3,4,5,6]
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = startNum; i < endNum; i++) {
list.add(i);
}
return list;
}
}
select * from(
select user_id,active_tag,dates,
-- 进行满足条件排序
row_number() over(order by dates asc) as rn
from zijie
where active_tag>15
union
select user_id,active_tag,dates,
-- 如果不为空,就返回空
if(
( row_number() over(order by dates asc) ) is not null,null,row_number() over(order by active_tag asc)
)as rn
from zijie
where active_tag<15
) as b order by b.dates asc
;
输出结果为:
输出结果为: