【问题标题】:Group by Issue GreenPlum按问题分组 GreenPlum
【发布时间】:2021-10-27 12:37:03
【问题描述】:

我想在大约 300 列的表中进行分组。 有订单,其状态会在接下来的 30 天内更新。 我想选择最大(更新时间)的订单。 所以我的查询是这样的:

select order_num,status,order_date,max(update_date)     from orders
where order_date = '2021-07-01'
     and update_date between '2021-07-01' and '2021-08-01'
 group by 'primary_key';

有没有办法编写查询而不在所有 300 列上添加聚合函数?

【问题讨论】:

  • 按 pk 分组没有意义

标签: sql greatest-n-per-group greenplum


【解决方案1】:

你正在寻找这个:

select * from (
   select * , row_number() over (partition by order_num order by update_date desc) rn
   from orders
) t
where rn = 1;

【讨论】:

    【解决方案2】:

    我想选择 max(update_time) 的订单。

    我想你想要:

    select o.*
    from orders o
    order by o.update_time
    offset 0 row fetch first 1 row only;
    

    fetch first 子句仅获取一行。您没有指定您的数据库,但您的数据库可能使用 limitselect top 或其他名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 2011-04-01
      • 2014-05-24
      • 2012-02-19
      • 2015-06-23
      • 1970-01-01
      相关资源
      最近更新 更多