我在Access中使用子查询创建了一个查询,并且无法在Excel 2003中链接它:当我使用菜单Data
- > Import External Data
- > Import Data...
并选择mdb文件,查询不在列表中。如果我使用菜单Data
- > Import External Data
- > New Database Query...
,我可以在列表中看到我的查询,但在导入向导的末尾我收到此错误:
Too few parameters. Expected 2.
我的猜测是查询语法导致问题,实际上查询包含子查询。因此,我将尝试描述查询目标和结果语法。
表格位置
表格货币
查询目标
查询
可以使用“设计视图”创建没有最终总和的查询。生成的SQL是:
SELECT Currency.code, Sum(Positions.position) AS SumOfposition
FROM [Currency] INNER JOIN Positions ON Currency.ID = Positions.currency_id
WHERE (((Positions.portfolio)=1))
GROUP BY Currency.code
HAVING (((Currency.code) In ("A","B")));
为了计算最终的SUM我做了以下(在SQL视图中):
SELECT Sum(Abs([temp].[SumOfposition])) AS sumAbs
FROM [SELECT Currency.code, Sum(Positions.position) AS SumOfposition
FROM [Currency] INNER JOIN Positions ON Currency.ID = Positions.currency_id
WHERE (((Positions.portfolio)=1))
GROUP BY Currency.code
HAVING (((Currency.code) In ("A","B")))]. AS temp;
所以,问题是:是否有更好的方法来构建查询以使导出工作?
答案 0 :(得分:1)
我看不出太多错误,但是我会把一些垃圾访问放入并缩小查询到这个,希望这应该运行正常:
SELECT Sum(Abs(A.SumOfPosition)) As SumAbs
FROM (SELECT C.code, Sum(P.position) AS SumOfposition
FROM Currency As C INNER JOIN Positions As P ON C.ID = P.currency_id
WHERE P.portfolio=1
GROUP BY C.code
HAVING C.code In ("A","B")) As A
答案 1 :(得分:0)
尝试在MS Access查询定义中声明参数并定义其数据类型可能是值得的。当您尝试在MS Access本身之外使用查询时,这一点尤其重要,因为它无法自动检测参数类型。这种方法有时会被击中或错过,但值得一试。
PARAMETERS [[Positions].[portfolio]] Long, [[Currency].[code]] Text ( 255 );
SELECT Sum(Abs([temp].[SumOfposition])) AS sumAbs
FROM [SELECT Currency.code, Sum(Positions.position) AS SumOfposition
FROM [Currency] INNER JOIN Positions ON Currency.ID = Positions.currency_id
WHERE (((Positions.portfolio)=1))
GROUP BY Currency.code
HAVING (((Currency.code) In ("A","B")))]. AS temp;
答案 2 :(得分:0)
由于外部查询正在做一个微不足道的事实,我已经解决了我的问题。在Excel中选择New Database Query...
时,在此过程结束时,在按Finish
后,会弹出Import Data
表单,询问
您想将数据放在哪里?
您可以点击Create a PivotTable report...
。如果正确定义数据透视表,Excel将仅显示外部总和。