按日期获取匹配结果

时间:2017-03-17 11:10:33

标签: sql-server database stored-procedures views

我有这个数据库: image

如何按日期选择匹配?我想要这样的输出:Barcelona(HomeTeam),2(HomeTeamScore),2(AwayTeamScore),PSG(AwayTeam)。

我想选择在某个日期进行的所有比赛,我需要从表PlayerMatch得到球队得分的球队的COUNT结果(在这种情况下,外键MatchId = 2(表PlayerMatch)梅西为巴塞罗那得分两球和卡文尼为PSG打进2球,结果将是2:2)。

以下是创建表格和输入一些数据的代码:

CREATE TABLE Team(
TeamId          INT             IDENTITY PRIMARY KEY,
Name            NVARCHAR(75)    NOT NULL,
);

CREATE TABLE Players
(
PlayerId int identity Primary Key,
Name nvarchar(50) NOT NULL,
TeamId int,
FOREIGN KEY (TeamId) REFERENCES Team(TeamId)
);

CREATE TABLE Match
(
MatchId int identity Primary Key,
MatchDate date NOT NULL,
HomeTeamId int NOT Null,
AwayTeamId int NOT NULL,
FOREIGN KEY (HomeTeamId) REFERENCES Team(TeamId),
FOREIGN KEY (AwayTeamId) REFERENCES Team(TeamId)
);

CREATE TABLE PlayerMatch
(
PlayerMatchId int identity Primary key,
Goal bit not null,
PlayerId int not null,
MatchId int not null,
FOREIGN KEY (PlayerId) REFERENCES Players(PlayerId),
FOREIGN KEY (MatchId) REFERENCES Match(MatchId),
);

INSERT INTO Team (Name) VALUES ('Liverpool');
INSERT INTO Team(Name) VALUES ('Real Madrid');
INSERT INTO Team(Name) VALUES ('Barcelona');
INSERT INTO Team(Name) VALUES('PSG');

INSERT INTO Players(Name, TeamId) VALUES('Roberto Firmino', 1);
INSERT INTO Players(Name, TeamId) VALUES('Adam Lallana', 1);
INSERT INTO Players(Name, TeamId) VALUES('Luka Modric', 2);
INSERT INTO Players(Name, TeamId) VALUES('Benzema', 2);
INSERT INTO Players(Name, TeamId) VALUES('Messi', 3);
INSERT INTO Players(Name, TeamId) VALUES('Arda Turan', 3);
INSERT INTO Players(Name, TeamId) VALUES('Cavani', 4);
INSERT INTO Players(Name, TeamId) VALUES('Lucas', 4);

INSERT INTO Match(MatchDate, HomeTeamId, AwayTeamId) VALUES ('06-06-2007', 1, 2);
INSERT INTO Match(MatchDate, HomeTeamId, AwayTeamId) VALUES ('06-07-2007', 3, 4);


INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 1, 1);
INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 1, 2);
INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 1, 3);

INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 2, 5);
INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 2, 5);
INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 2, 7);
INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 2, 7);

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

SELECT A.MATCHDATE, B1.NAME AS HOMETEAM, C1.GOAL AS GOAL_HOME, C2.GOAL AS GOAL_AWAY, B2.NAME AS AWAYTEAM
 FROM MATCH A
 INNER JOIN TEAM AS B1 ON A.HOMETEAMID=B1.TeamId
 INNER JOIN TEAM AS B2 ON A.AWAYTEAMID=B2.TeamId
 LEFT JOIN (SELECT MATCHID, TEAMID, COUNT(GOAL) AS GOAL
            FROM PLAYERMATCH 
            INNER JOIN PLAYERS ON PLAYERS.PlayerId=PLAYERMATCH.PlayerId
            GROUP BY MATCHID, TEAMID) C1 ON A.MATCHID = C1.MatchId AND A.HOMETEAMID=C1.TeamId
 LEFT JOIN (SELECT MATCHID, TEAMID, COUNT(GOAL) AS GOAL
            FROM PLAYERMATCH 
            INNER JOIN PLAYERS ON PLAYERS.PlayerId=PLAYERMATCH.PlayerId
            GROUP BY MATCHID, TEAMID) C2 ON A.MATCHID = C2.MatchId AND A.AWAYTEAMID=C2.TeamId

输出结果为:

MATCHDATE  HOMETEAM                                                                    GOAL_HOME   GOAL_AWAY   AWAYTEAM
---------- --------------------------------------------------------------------------- ----------- ----------- ---------------------------------------------------------------------------
2007-06-06 Liverpool                                                                   2           1           Real Madrid
2007-07-06 Barcelona                                                                   2           2           PSG