如何获取可能在两行之一中找到的值的计数

coo*_*e89 1 sql postgresql merge

如果我有一个有价值观的桌子

Game Id | Home     | Away   |
------- | -------- |------- |
0       | Team A   | Team B |
1       | Team C   | Team D |
2       | Team B   | Team C |
3       | Team D   | Team C |
Run Code Online (Sandbox Code Playgroud)

在SQL中,无论是Home还是Away,我如何得到每个团队的Count.

例如

    Team    | Count   
    ------- | ----- 
    Team A  | 1 
    Team B  | 2   
    Team C  | 3   
    Team D  | 2 
Run Code Online (Sandbox Code Playgroud)

我在python中的hack是分成两个计数表,并将表合并在一起,但我认为有更好的方法在SQL中执行此操作

Gor*_*off 5

在SQL中,您将使用union allgroup by:

select team, count(*)
from ((select home as team from t) union all
      (select away from t)
     ) t
group by team;
Run Code Online (Sandbox Code Playgroud)