首页 > 解决方案 > 只选择一个列,例如玩家,首先选择一个最大值,然后选择一个最小值

问题描述

所以我有两张表“AllBowlRecords”和一张“AggregateBowlRecords”

AllBowlRecords:-

plr_fullnm|Wkts|Runs
---------------------
Bumrah    |  4 | 23
Bumrah    |  2 | 7
Bumrah    |  1 | 51
Bumrah    |  4 | 39
Jason     |  3 | 48
Jason     |  3 | 29
Jason     |  3 | 70

所以我想要的只是根据 Wkts 为 MAX 的 AllBowlRecords 更新 AggregateBowlRecords,但如果多次出现 MAX Wkts 值,则应选择对应的最小运行次数。AggregateBowlRecords 应该是这样的:

Bumrah    |  4 | 23
Jason     |  3 | 29

有哪些可能的解决方案?

标签: sql

解决方案


您可以使用以下查询返回所需的结果row_number()

select plr_fullnm, Wkts, Runs
from (select abr.*,
             row_number() over (partition by plr_fullnm order by wkts desc, runs) as seqnum
      from AllBowlRecords abr
     ) abr
where seqnum = 1;

推荐阅读