gpt4 book ai didi

mysql - 来自不同列的分组值和计数

转载 作者:行者123 更新时间:2023-11-29 02:03:18 24 4
gpt4 key购买 nike

我有这张表:

UNIQUE_ID | WINNER_ID | FINALIST_ID
________1 | ________1 | __________2
________2 | ________1 | __________3
________3 | ________3 | __________1
________4 | ________1 | __________2

我需要一份所有玩家(获胜者和决赛入围者)的列表以及他们获得第一名或第二名的次数。

在这种情况下,它将是:

PLAYER_ID | WINNER_TIMES | FINALIST_TIMES
________1 | ___________3 | _____________1
________2 | ___________0 | _____________2
________3 | ___________1 | _____________1

这里已经问过类似的问题 ( LINK ),但我不明白答案。

最佳答案

select  coalesce(winner_id, finalist_id) as PLAYER_ID
, count(winner_id) as WINNER_TIMES
, count(finalist_id) as FINALIST_TIMES
from (
select winner_id
, null as finalist_id
from YourTable
union all
select null
, finalist_id
from YourTable
) as SubQueryAlias
group by
coalesce(winner_id, finalist_id)

Live example at SQL Fiddle.

关于mysql - 来自不同列的分组值和计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11073646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com