2 回答
您有单独的查询来计算10DayTotalVolume
和10DayAvgVolume
。我怀疑您可以在一个查询qry10DayVolumes中计算两者。
SELECT
b.SYMBOL,
Sum(b.VOLUME) AS 10DayTotalVolume,
Avg(b.VOLUME) AS 10DayAvgVolume
FROM
[tbl-B] AS b INNER JOIN
qryLast10DaysStored AS q
ON b.TIMESTAMP = q.TIMESTAMP
GROUP BY b.SYMBOL;
然而,这让我想知道10DayAvgVolume
除了10DayTotalVolume / 10
类似的考虑适用于 15 天和 30 天的值。
最终,我认为你想要一些基于这样的起点的东西:
SELECT
q10.SYMBOL,
q10.[10DayTotalVolume],
q10.[10DayAvgVolume],
q15.[15DayTotalVolume],
q15.[15DayAvgVolume],
q30.[30DayTotalVolume],
q30.[30DayAvgVolume]
FROM
(qry10DayVolumes AS q10
INNER JOIN qry15DayVolumes AS q15
ON q10.SYMBOL = q15.SYMBOL)
INNER JOIN qry30DayVolumes AS q30
ON q10.SYMBOL = q30.SYMBOL;
这假设您已经创建qry15DayVolumes
并qry30DayVolumes
遵循我建议的方法qry10DayVolumes
。
如果您想减少查询的数量,您可以为每个已qry??DayVolumes
保存的查询使用子查询,但首先尝试这种方式以确保逻辑正确。
在上面的第二个查询中,由于字段名称以数字开头,可能会出现问题。将这些名称括在方括号中或将它们重新命名为qry10DayVolumes
, qry15DayVolumes
, 并qry30DayVolumes
使用以字母而不是数字开头的别名。
我使用您上传的“ 2nd Upload.mdb ”测试了上面编写的查询,它从 Access 2007 运行时没有出现错误。这是该查询结果集的第一行:
SYMBOL 10DayTotalVolume 10DayAvgVolume 15DayTotalVolume 15DayAvgVolume 30DayTotalVolume 30DayAvgVolume
ACC-1 42909 4290.9 54892 3659.46666666667 89669 2988.96666666667
Access 不支持最高级的 SQL 语法和子句,所以这有点小技巧,但它可以工作,并且在您的小样本上速度很快。您基本上正在运行 3 个查询,但 Union 子句允许您合并为一个:
select
Symbol,
sum([10DayTotalVol]) as 10DayTotalV,
sum([10DayAvgVol]) as 10DayAvgV,
sum([15DayTotalVol]) as 15DayTotalV,
sum([15DayAvgVol]) as 15DayAvgV,
sum([30DayTotalVol]) as 30DayTotalV,
sum([30DayAvgVol]) as 30DayAvgV
from (
select
Symbol,
sum(volume) as 10DayTotalVol, avg(volume) as 10DayAvgVol,
0 as 15DayTotalVol, 0 as 15DayAvgVol,
0 as 30DayTotalVol, 0 as 30DayAvgVol
from
[tbl-b]
where
timestamp >= (select min(ts) from (select distinct top 10 timestamp as ts from [tbl-b] order by timestamp desc ))
group by
Symbol
UNION
select
Symbol,
0, 0,
sum(volume), avg(volume),
0, 0
from
[tbl-b]
where
timestamp >= (select min(ts) from (select distinct top 15 timestamp as ts from [tbl-b] order by timestamp desc ))
group by
Symbol
UNION
select
Symbol,
0, 0,
0, 0,
sum(volume), avg(volume)
from
[tbl-b]
where
timestamp >= (select min(ts) from (select distinct top 30 timestamp as ts from [tbl-b] order by timestamp desc ))
group by
Symbol
) s
group by
Symbol