【问题标题】:How to get max of group by query如何通过查询获得最大组
【发布时间】:2020-01-03 04:07:33
【问题描述】:

这是我的数据集:

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 ;

这可能有什么问题?

【问题讨论】:

  • 嗨 Sumith - 有点不清楚,请您添加列名。另外,您的查询得到什么输出

标签: hive cqlsh


【解决方案1】:

您需要从 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 ;

【讨论】:

    猜你喜欢
    • 2012-04-02
    • 2021-12-12
    • 2015-10-07
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多