在MS Access中将不同的查询连接到1个查询中

时间:2016-06-02 08:32:35

标签: database ms-access

在过去的几天里,我一直在尝试我所知道的一切以及我能在互联网上找到的一切。但我似乎无法弄明白,这让我变成了香蕉。

我有3种不同的MS Access查询。

  

QUERY 1

SELECT First([Master Data Results 2013 - 2016].[Winner]) AS Player, Count([Master Data Results 2013 - 2016].[Winner]) AS Won
FROM [Master Data Results 2013 - 2016]
GROUP BY [Master Data Results 2013 - 2016].[Winner]
HAVING (((Count([Master Data Results 2013 - 2016].[Winner]))>1));  

此查询计算玩家赢得的每场网球比赛3年的周期

  

QUERY 2

SELECT First([Master Data Results 2013 - 2016].[Loser]) AS Player, Count([Master Data Results 2013 - 2016].[Loser]) AS Lost
FROM [Master Data Results 2013 - 2016]
GROUP BY [Master Data Results 2013 - 2016].[Loser]
HAVING (((Count([Master Data Results 2013 - 2016].[Loser]))>1));

此查询会对玩家因网络游戏丢失3年的网球比赛进行对抗

  

QUERY 3

INSERT INTO Master ( Lost )
SELECT Winner.*, Loser.Lost
FROM Loser INNER JOIN Winner ON Loser.Player = Winner.Player;

我创建了一个" Master"表。在此表中,我想将QUERY1和QUERY 2的组合结果放在一起。

Master表看起来像这样。

enter image description here

所以我想要在Master表的Players列中的QUERY 1中的玩家。 这些数字是Master表中Won列中QUERY 1的结果,而Master表中Lost列中的QUERY 2计算的数字。

当我单独运行每个查询而不是它的工作时,我想将所有这些放入1个查询中以便于使用。

1 个答案:

答案 0 :(得分:1)

您可以使用UNION查询来组合查询1和2的结果,查询1 = 0中的列[Lost],查询2 = 0中的[Won]列,然后使用[Lost]上的Sum和[]累加此查询韩元]。像这样:

INSERT INTO Master (Player, Won, Lost)
SELECT Player, Sum(Won), Sum(Lost) FROM (
    SELECT First([Master Data Results 2013 - 2016].[Winner]) AS Player,
    Count([Master Data Results 2013 - 2016].[Winner]) AS Won,
    0 AS Lost
    FROM [Master Data Results 2013 - 2016]
    GROUP BY [Master Data Results 2013 - 2016].[Winner]
    HAVING (((Count([Master Data Results 2013 - 2016].[Winner])) > 1))

    UNION

    SELECT First([Master Data Results 2013 - 2016].[Loser]) AS Player,
    0 as Won,
    Count([Master Data Results 2013 - 2016].[Loser]) AS Lost
    FROM [Master Data Results 2013 - 2016]
    GROUP BY [Master Data Results 2013 - 2016].[Loser]
    HAVING (((Count([Master Data Results 2013 - 2016].[Loser])) > 1))
)
GROUP BY Player