如何根据值 select 区分具有偏好的记录

How to select distinct records with preference depending on a value

我有这个table: Table 1

注意有些 id 有双记录且 IsTop = 1 如果我遇到这种情况,我有兴趣选择 IsTop = 1 的场景,如果我不想保留 IsTop = 0 的场景。

目标是拥有不同的 Id,但 IsTop = 1 是否存在。

我该怎么做?

您可以使用 row_number():

select t.*
from (select t.*,
             row_number() over (partition by id order by isTop desc) as seqnum
      from t
     ) t
where seqnum = 1;