根据表中每个组的唯一列选择行

问题描述

----------------
c_id  s_id   p_id    (customer_id,service_id,provider_id)
---- ---- ------  
1,1,1 - not elegible to select as next entry has same p_id
1,2,1
1,3,3

2,1
2,2
2,3

3,3 - not elegible to select as next entry has same p_id
3,3

编写查询以从上述数据中产生以下结果的低成本方法是什么?

 ----------------
 c_id  s_id  p_id 
 ---- ---- ------ 
  1,1
  1,3

  2,1
  2,2
  2,3

  3,3

解决方法

在MySQL 8.0中,您可以使用lead()来检索“下一个” p_id,并使用该信息来筛选出pid与下一个值相同的行。 / p>

select *
from (
    select t.*,lead(p_id) over(partition by c_id order by s_id) lead_p_id
    from mytable t
) t
where not p_id <=> lead_p_id

在早期版本中,通常会使用相关子查询:

select t.*
from mytable t
where not pid <=> (
    select p_id
    from mytable t1
    where t1.c_id = t.c_id and t1.s_id > t.s_id
    order by t1.s_id
    limit 1
)
,

我不太确定这是否最具成本效益,但它似乎是我能想到的最明显的解决方案。

select 
   c_id,max(s_id) [s_id],p_id
from
  `table_name`
group by
   c_id,p_id
,

如果s_id是无间隔的序列,则可以使用:

select t.*
from t left join
     t tnext
     on tnext.c_id = t.c_id and tnext.s_id = t.sid + 1
where not (tnext.p_id <=> t.p_id);