hive 使用partion by获取分组后的首尾时间

insert overwrite table smes_trans.trans_reel_supplier
select 
distinct 
queue_tri.wo_code,
queue_tri.ws_id,
queue_tri.slot_no,
queue_tri.slot_side,
compare_results.mcno,
compare_results.bom_cpid,
queue_tri.slot_reel_id,
c.internal_parts_code,
queue_tri.inputtime,
queue_tri.outputtime,
c.supplier_date_code,
d.supplier_name
from 
(select distinct wo_code from smes_source.T_PCB_WO_WS_QUEUE ) as wo
left join 
(
select * from  (
select 
wo_code,
ws_id,
slot_no,
slot_side,
slot_reel_id,
first_value(date_time) over(partition by wo_code,ws_id,slot_no,slot_side,slot_reel_id order by date_time) inputtime,
last_value(date_time) over(partition by wo_code,ws_id,slot_no,slot_side,slot_reel_id order by date_time rows between unbounded preceding and unbounded following) outputtime,

rank()over(partition by wo_code,ws_id,slot_no,slot_side,slot_reel_id order by date_time asc) as rownumber
from smes_source.t_wo_slot_queue_tri 
               ) as oo  
where oo.rownumber=1
 )  as queue_tri
 on wo.wo_code=queue_tri.wo_code
left join  smes_source.f_workstation_hbase  as f_workstation
on queue_tri.ws_id=f_workstation.workstation_id
left join smes_source.w_offline_compare_results as compare_results
on queue_tri.wo_code=compare_results.wo_code
and queue_tri.slot_no=compare_results.slot_no
and queue_tri.slot_side=compare_results.slot_side
and f_workstation.mcno=compare_results.mcno
left join smes_source.r_reel_hbase b
on queue_tri.slot_reel_id=b.reel_id
left join smes_source.r_reel_received c
on b.batch_id=c.batch_id 
left join smes_source.m_supplier_hbase d
on c.supplier_id=d.supplier_id

讲解:

1.first_value(date_time) over(partition by 字段 order by date_time) 用于获取根据字段分组后取出开始时间

2.last_value(date_time) over(partition by 字段 by date_time rows between unbounded preceding and unbounded following) outputtime 用于获取分组排序后的最大数据,rows between unbounded preceding and unbounded following 是窗口函数表示在在date_time 这个日子之前和之后的数据。

3.rank()over(partition by wo_code,ws_id,slot_no,slot_side,slot_reel_id order by date_time asc) as rownumber 分组取对应的行号

你可能感兴趣的:(hive)