I have an simplified example of my data:
| Soccer team | Player | Goals |
| ------------ | ------------ | ------------ |
| Man. City | Haaland | 3 |
| Man. City | De Bruyne | 0 |
| Man. City | Foden | 0 |
| Man. Utd | Hojlund | 0 |
| Man. Utd | Onana | 0 |
| Man. Utd | Maguire | 0 |
And now I want to add a column that checks if a team has any goalscorers. So my result should be:
Soccer team | Player | Goals | Goals in team |
---|---|---|---|
Man. City | Haaland | 3 | y |
Man. City | De Bruyne | 0 | y |
Man. City | Foden | 0 | y |
Man. Utd | Hojlund | 0 | n |
Man. Utd | Onana | 0 | n |
Man. Utd | Maguire | 0 | n |
How could I achieve this? Which SQL statement should i use. Remember I have like 25 other columns. Should I still put it in an aggregation function and group by all of the other columns, or are there easier ways?
2条答案
按热度按时间q9rjltbz1#
You can use
SUM()
withCASE
statement.u0njafvf2#
You can use a window function inside a CASE expression for this. I've put * in the query because you said you had lots of other columns. You should replace the star with the list of columns you want in the query. You can also put columns after the 'goals_in_team' column if you prefer.