Oracle选择和分组依据

pxq42qpu  于 2023-06-22  发布在  Oracle
关注(0)|答案(1)|浏览(114)

我有下面的表格。

create table cricket(team1 varchar2(22),team2 varchar2(22));

select * from cricket

团队1团队2

INDIA  SL
SL    AUS
SA    ENG
ENG   NZ
AUS   INDIA

在上面的表格中,我想显示参加比赛的球队的数量。例如

India 2
SL    1
Sa    1
Eng   2
Aus   2
juud5qan

juud5qan1#

你可以UNION ALL这两列,然后计算:

WITH ALL_TEAMS AS (
    SELECT TEAM1 AS TEAM FROM CRICKET
    UNION ALL 
    SELECT TEAM2 FROM CRICKET
)
SELECT 
    TEAM, 
    COUNT(1) AS NUMBER_OF_PLAYS
FROM ALL_TEAMS
GROUP BY TEAM

相关问题