Having trouble with outer joins/sub queries
In MSSQL
I have a table which lists tournaments a golfer has fields season, tournament, player, position
e.g
season开发者_如何学编程 tournament player position
-------------------------------------
1983 A Mr Par 20
1983 B Mr Par 1
1983 C Mr Par 1
1984 A Mr Par 17
1984 B Mr Par 2
1985 A Mr Par 1
1985 C Mr Par 8
I want to create a query in MSSQL which will just show me the wins by year, inc where he did not win at all
i.e.
season player numWins
------------------------
1983 Mr Par 2
1984 Mr Par 0
1985 Mr Par 1
Can't seem to get correct result but know it is out there somewhere.
SELECT season, player,
SUM(CASE WHEN tour_position = 1 THEN 1 ELSE 0 END) AS WINS
FROM your_table
GROUP BY season, player
ORDER BY season, player
How about this:
SELECT [Season], COUNT(*)
FROM [YourTable]
WHERE [Position] = 1
GROUP BY [Season]
精彩评论