结合聚合查询

时间:2015-01-23 17:57:00

标签: sql ms-access

我有两张相关数据表。

  • 表1 - 经纪人的所有交易记录
  • 表2 - 所有交易中断(有错误/差异/问题的交易)

我创建了一个查询总计来自表1的经纪人的交易数量

我创建了一个Query to Total的数量" Breaks"来自表2的经纪人

然后我创建了一个Query来组合前两个查询并生成一些统计信息

示例:

Broker       Total Trades        Total Breaks       Break %
Goldman         10                    4               40%
Morgan          10                    2               20%

而不是创建3个查询 - 有没有办法创建1个查询来实现相同的结果?我希望执行更详细的分析/报告,而不会通过大量的单个查询来淹没数据库。下面的SQL代码

首先查询:

SELECT DISTINCTROW [All Breaks].Broker, Sum([All Breaks].TradeCount) AS   
[Sum Of TradeCount]
FROM [All Breaks]
GROUP BY [All Breaks].Broker;

第二次查询:

SELECT DISTINCTROW [All Trades].Broker, Sum([All Trades].TradeCount) AS  
SumOfTradeCount
FROM [All Trades]
GROUP BY [All Trades].Broker;

结果结果:合并

SELECT [Broker List].Broker, [All Breaks Query].[Sum Of TradeCount], [All  
Trades Query].SumOfTradeCount, [Sum Of TradeCount]/[SumOfTradeCount] AS  
Percentage
FROM ([Broker List] INNER JOIN [All Breaks Query] ON [Broker List].Broker  
= [All Breaks Query].Broker) INNER JOIN [All Trades Query] ON [Broker  
List].Broker = [All Trades Query].Broker;

非常感谢!

1 个答案:

答案 0 :(得分:0)

在查询设计中,有一种方法可以切换到SQL View,您可以在其中自由编写sql。

select one, two from (select one, joinfield from table1) as first Inner join (select two, joinfield from table2) as second ON first.joinfield = second.joinfield.

另请参阅Combining two MS Access queries