如何通过查询获取组的最大值

rnmwe5a2  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(423)

这是我的数据集:

00000000040112    2702      00000000040112  AVAILABLE       1566921227223   -6.0    LB
00000000040112    2702      00000000040112  AVAILABLE       1566921247222   -9.0    LB
00030400791888    6065      00030400791888  AVAILABLE       1566919357992   45.0    EA
00030400791888    6065      00030400791888  AVAILABLE       1566919547809   72.0    EA

我试图从每组中得到最大值,因此根据上述数据,预期结果如下:

00000000040112  2702    00000000040112  AVAILABLE       1566921247222   -9.0    LB 
00030400791888  6065    00030400791888  AVAILABLE       1566919547809   72.0    EA

我的查询没有产生正确的结果:

select  
  primegtin, nodeid, gtin, inventory_state, 
  max(last_updated_time), 
  quantity_by_gtin, quantity_uom 
from pit_by_prime_gtin 
where 
  year=2019 and month =8 and day =27 and hour=15 
group by 
  primegtin, nodeid, gtin, inventory_state, 
  last_updated_time, 
  quantity_by_gtin, quantity_uom ;

有什么问题吗?

o4hqfura

o4hqfura1#

您需要删除要从中进行聚合的列 group by 条款。
在您的示例中,它可能是这样的:

select  
  primegtin, nodeid, gtin, inventory_state, 
  max(last_updated_time), 
  max(quantity_by_gtin), quantity_uom 
from pit_by_prime_gtin 
where 
  year=2019 and month =8 and day =27 and hour=15 
group by 
  primegtin, nodeid, gtin, inventory_state, 
  quantity_uom ;

相关问题