使用单个表查询Access以计算以前的Basket结果数据库

时间:2015-10-28 19:29:11

标签: ms-access subquery

我必须对Access 2010数据库进行查询,以便为每个团队提取以前的分数结果。

我在Basket上有一张包含历史记录匹配结果的表格

The table is like this :
Table Name = History
ID Auto Increment
Date : datetime
Host : text
Guest: text
HostPoint:integer
GuestPoint:inetger

表格数据如下:

1     01/02/2015     BARCELONA     REAL MADRID     105  80
5     25/01/2015     BARCELONA     LAS PALMAS      65   73
7     24/01/2015     BARCELONA     JUVENTUS        80   75
10     15/01/2015    VALENCIA      REAL MADRID     101  120
11     14/01/2015    TOLEDO        REAL MADRID     101  100
15     14/01/2015    BARCELONA     PSG             105  60
18     11/01/2015    GALA          REAL MADRID     101  70

我想为每条记录生成一个表或查询,每个团队在主场的前3场比赛中的最小和最大结果,以及客人的比赛。

表格如下:

Table Name = HistoryResult
ID Auto Increment
Date : datetime
Host : text
Guest: text
HostPoint:integer
GuestPoint:inetger
MinHostHomePoint:integer
MaxHostHomePoint:integer
MinGuestAwayPoint:inetger
MaxGuestAwayPoint:inetger

示例,对于第一条记录,结果必须是:

1     01/02/2015     BARCELONA     REAL MADRID     105  80    65  105   70  120
5     25/01/2015     BARCELONA     LAS PALMAS      65   73    80  105   0   0
7     24/01/2015     BARCELONA     JUVENTUS        80   75   105  105   0   0
10     15/01/2015    VALENCIA      REAL MADRID     101  120  0    0     70  100
11     14/01/2015    TOLEDO        REAL MADRID     101  100  0    0     70  70

我认为我必须使用Alias和Subquery,但我尝试了不同的解决方案来解决问题

1 个答案:

答案 0 :(得分:0)

尝试以下方法;

SELECT History.ID, History.MatchDate, History.Host, History.Guest, History.HostPoint, History.GuestPoint, qryHomeMinMaxPoints.MinOfPoints AS MinHostHomePoint, qryHomeMinMaxPoints.MaxOfPoints AS MaxHostHomePoint, qryAwayMinMaxPoints.MinOfPoints AS MinGuestAwayPoint, qryAwayMinMaxPoints.MaxOfPoints AS MaxGuestAwayPoint FROM (History INNER JOIN (SELECT Team, Min(Points) AS MinOfPoints, Max(Points) AS MaxOfPoints FROM (SELECT MatchDate, Team, Points FROM (SELECT History.MatchDate, History.Host AS Team, History.HostPoint AS Points FROM History)  AS qryHomeMatches WHERE MatchDate In  (SELECT TOP 3 MatchDate  FROM   ( SELECT History.MatchDate, History.Host AS Team, History.HostPoint AS Points FROM History )   as qryHomeMatchesCopy  WHERE qryHomeMatchesCopy.Team = qryHomeMatches.Team  ORDER By MatchDate DESC))  AS qryHomeLastThree GROUP BY Team)  AS qryHomeMinMaxPoints ON History.Host=qryHomeMinMaxPoints.Team) INNER JOIN (SELECT Team, Min(Points) AS MinOfPoints, Max(Points) AS MaxOfPoints FROM (SELECT MatchDate, Team, Points FROM (SELECT MatchDate, Guest AS Team, GuestPoint AS Points FROM History)  AS qryAwayMatches WHERE MatchDate In  (SELECT TOP 3 MatchDate  FROM   ( SELECT MatchDate, Guest AS Team, GuestPoint AS Points FROM History )  as qryAwayMatchesCopy  WHERE qryAwayMatchesCopy.Team = qryAwayMatches.Team  ORDER By MatchDate DESC))  AS qryAwayLastThree GROUP BY Team)  AS qryAwayMinMaxPoints ON History.Guest=qryAwayMinMaxPoints.Team;