【问题标题】:I need to get last created eligible rider ids and pinged rider ids accordeing to a orderId using a sql query我需要使用 sql 查询根据订单 ID 获取最后创建的合格骑手 ID 和 ping 骑手 ID
【发布时间】:2021-09-22 04:51:46
【问题描述】:

我需要把我的数据集作为这个表

我正在尝试获得这样的合格集,还需要 group_concat ping 集

x.id IN (SELECT MAX(x.id) FROM x WHERE ping rider id IS NULL GROUP BY orderId)

【问题讨论】:

标签: mysql sql mysql-workbench


【解决方案1】:

您可以根据eligible_riders 中非空值的累积数量来分配组。然后聚合并取最后一个值:

select og.*
from (select order_id, grp, max(eligible_riders) as eligible_riders,
             group_concat(rider_id) as riders,
             row_number() over (partition by order_id order by min(id) desc) as seqnum
      from (select t.*,
                   sum(eligible_riders <> '') over (partition by order_id order by id) as grp
            from t
           ) t
      group by order_id, grp
     ) og
where seqnum = 1;

嗯。 . .您也可以使用相关子查询来执行此操作,这可能看起来更简单一些:

select order_id, max(eligible_riders) as eligible_riders,
       group_concat(rider_id) as riders
from t
where t.id >= (select max(t2.id)
               from t t2
               where t2.order_id = t.order_id and
                     t2.eligible_riders <> ''
              )
group by order_id;

为了提高性能,您需要在(order_id, eligible_riders) 上建立索引。

【讨论】:

  • 此查询需要超过 30 秒才能执行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 2015-06-11
  • 1970-01-01
  • 2017-08-28
  • 2014-03-12
  • 1970-01-01
  • 2017-10-27
相关资源
最近更新 更多