我之前尝试过问这个问题,现在我找到了一种更加统一的方式来提问这个问题。
我在Access 2007中工作。
我在Access中查询了以下格式:
SELECT RecDate, Weight * YTD / (SELECT SUM(Weight) From Records
WHERE Class1=t2.Class1 AND Class2 = t2.Class2) as AdjYield
From Records as t2
WHERE Class1="fi" AND Class2="ra"
它给了我以下结果
RecDate AdjYield
11/27/2012 4.73704622309864E-05
11/27/2012 0
11/27/2012 -1.87533613791046E-04
11/27/2012 -2.4014644030909E-05
11/27/2012 1.29986083977377E-04
11/27/2012 1.2765190817936E-04
12/11/2012 1.10288780895771E-04
12/11/2012 0
12/11/2012 -1.7463943470934E-04
12/11/2012 -1.51778890534467E-05
12/11/2012 1.47374111584102E-04
12/11/2012 1.45120288096115E-04
现在我想要每天的SUM计算出的AdjYield
RecDate AdJYield
11/27/2012, 1.557E-05
12/11/2012, 3.549E-05
我尝试在Weight * YTD / (SELECT SUM(Weight) From Records WHERE Class1=t2.Class1 AND Class2 = t2.Class2) as AdjYield
部分周围放置SUM(),然后在最后添加GROUP BY RecDate
。但是我收到错误说
您尝试执行的查询不包含指定表达式“Class1”作为聚合函数的一部分。
关于如何正确聚合的任何想法?
答案 0 :(得分:2)
将您现有的查询用作GROUP BY RecDate
的其他人的子查询。
SELECT sub.RecDate, Sum(sub.AdjYield) AS SumOfAdjYield
FROM
(
SELECT RecDate, Weight * YTD / (SELECT SUM(Weight) From Records
WHERE Class1=t2.Class1 AND Class2 = t2.Class2) as AdjYield
From Records as t2
WHERE Class1="fi" AND Class2="ra"
) AS sub
GROUP BY sub.RecDate;
答案 1 :(得分:0)
如果您只有一个包含这些字段的表:RecDate AdjYield在一个名为records的表中,您
should be able to say
SELECT SUM(AdjYield)
FROM Records
Group by RecDate
如果情况并非如此,请澄清。