Вот ваше решение в «одном» выражении, логически разделенном в утверждении WITH:
DECLARE @Meetings TABLE (
HomeTeam VARCHAR(100) NOT NULL,
AwayTeam VARCHAR(100) NOT NULL,
Home INT NOT NULL,
Away INT NOT NULL
)
INSERT INTO @Meetings
(
HomeTeam,
AwayTeam,
Home,
Away
)
VALUES
('A', 'B', 2, 5),
('C', 'D', 1, 5),
('A', 'D', 3, 2),
('C', 'A', 1, 5),
('C', 'B', 4, 2),
('B', 'D', 1, 4)
;WITH Teams AS (
SELECT DISTINCT HomeTeam AS TeamName FROM @Meetings
UNION
SELECT DISTINCT AwayTeam FROM @Meetings
)
, TeamWins AS (
SELECT T.TeamName, (HW.HomeWins + AW.AwayWins) Wins, GM.Games FROM Teams T
OUTER APPLY
(SELECT COUNT(*) HomeWins FROM @Meetings M WHERE M.HomeTeam = T.TeamName AND M.Home > M.Away) HW
OUTER APPLY
(SELECT COUNT(*) AwayWins FROM @Meetings M WHERE M.AwayTeam = T.TeamName AND M.Away > M.Home) AW
OUTER APPLY
(SELECT COUNT(*) Games FROM @Meetings M WHERE M.AwayTeam = T.TeamName) GM
)
SELECT * FROM TeamWins TW WHERE 2*TW.Wins > TW.Games
Вывод:
TeamName Wins Games
A 2 1
C 1 0
D 2 3