我在用PostgreSQL。
我有一个桌上的竞争对手,上面有一个电子竞技游戏中的竞争对手的信息。该表包含gameID、竞争对手的昵称和他/她所扮演角色的名称。
我想选择每个尼克和他们扮演过的角色。
例如,如果竞争对手假扮角色Ryze 4次,Lulu 3次,我希望假扮角色Ryze 4出现在输出中。
这就是我目前所拥有的:
select nick, character, count(*) as played
from competitor
group by nick, character
order by nick;
但是我得到了伪造的Ryze4和伪造的Lulu3的输出。
我试过使用max()和嵌套,但我搞不懂。
最佳答案
试试这个:
with counts as (
select nick, character, count(*) cnt, max(sometimestamp) hora
from competitor
group by nick, character
order by nick, cnt desc, hora
)
select nick, character, max(cnt)
from counts c1 where nick
in (select nick from counts where nick = c1.nick limit 1)
and character in (select character from counts where nick = c1.nick limit 1)
group by nick, character
order by nick
max(sometimestamp) hora
将会打破平局,你必须适应。或者如果你不喜欢领带,就把这个区域去掉。此查询将为每个缺口提供一个字符,其计数越大,如果出现平局,它将返回第一个达到最大计数的缺口。