hive查询:根据优先级和日期计算最大指示符值

qlzsbp2j  于 2021-06-27  发布在  Hive
关注(0)|答案(1)|浏览(297)

我试图框架的查询,但不知何故没有得到所需的结果,因此张贴。我是新来的Hive。如果事情很简单,我道歉。
源数据:

Ik - priority - ind1 - ind2 - date
1 -   A -           y -       n   -    2009/01/01
1 -   B -           n -       y  -     2019/02/09
1 -   C -          null -     (empty)- 2018/05/07
2 -   A -          null -     y -      2005/02/02
2 -   B -          null -     y -      2006/05/05
2 -   C -           n -       null -   2018/01/01

问题陈述
根据优先级和日期,我们需要为每个ik填充指示符值(ind1和ind2)。
输出表格式
ik,ind1,ind2
逻辑是
这里的分组将在ik场上进行。所以对于上面的数据集,在输出中将只有一条记录被填充。
如果对于相同的ik值,优先级为a,指示标志(ind1,ind2)为y值,则输出应填充为“y”。
但如果相同的ik,优先级是一个指示器,但没有值“y”(可能的值为null、n、空字符串)
然后将根据b c priority中的日期字段(order by date-latest record group by ik)选择latest indicator。
上述数据集的输出为

Ik - ind1 - ind2
1 -    y     -   y
2 -    n    -   y

这里ind1是max(ind1)。我能推导出。但无法导出ind2。
你能帮我创建查询吗?

eivnm1vs

eivnm1vs1#

测试数据:http://demo.gethue.com/hue/editor?editor=293916

with your_table as -------use your table instead of this subquery
(
select stack(6,

1 ,'A',  'y','n',    '2009/01/01',
1 ,'B',  'n','y',    '2019/02/09',
1 ,'C', null,'' ,    '2018/05/07',
2 ,'A', null,'y',    '2005/02/02',
2 ,'B', null,'y',    '2006/05/05',
2 ,'C',  'n', null,  '2018/01/01'

) as (Ik, priority, ind1, ind2, date)
) -------use your table instead of this subquery

select ik, 
       max(case when priority ='A' and ind1='y' then 'y' else last_ind1 end) ind1,
       max(case when priority ='A' and ind2='y' then 'y' else last_ind2 end) ind2
from
(
select Ik, priority, ind1, ind2, date,
       last_value(ind1) over (partition by Ik order by date) last_ind1,
       last_value(ind2) over (partition by Ik order by date) last_ind2
  from your_table -------use your table instead 
)s
group by ik;

结果:

ik  ind1    ind2
1   y   y
2   n   y

相关问题