I have created a query with a subquery in Access, and cannot link it in Excel 2003: when I use the menu Data
-> Import External Data
-> Import Data...
and select the mdb file, the query is not present in the list. If I use the menu Data
-> Import External Data
-> New Database Query...
, I can see my query in the list, but at the end of the import wizard I get this error:
Too few parameters. Expected 2.
My guess is that the query syntax is causing the problem, in fact the query contains a subquery. So, I'll try to describe the query goal and the resulting syntax.
Table Positions
- ID (Autonumber, Primary Key)
- position (double)
- currency_id (long) (references Currency.ID)
- portfolio (long)
Table Currency
- ID (Autonumber, Primary Key)
- code (text)
Query Goal
- Join the 2 tables
- Filter by portfolio = 1
- Filter by currency.code in ("A", "B")
- Group by currency and calculate the sum of the positions for each currency group an call the result: sumOfPositions
- Calculate abs(sumOfPositions) on each currency group
- Calculate the sum of the previous results as a single result
Query
The query without the final sum can be created using the Design View. The resulting SQL is:
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")));
in order to calculate the final SUM I did the following (in the SQL View):
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;
So, the question is: is there a better way for structuring the query in order to make the export work?