N
nimajneb
Hello! This is my first post on to Access Monster. I have a specific
question with general implications. I've simplified it, since the real code
is pretty gargantuan.
The database... The Access database I set up has a table called Agents,
another table called Auto_NB, and another one called Fire_NB. Auto_NB has
various fields, such as Agent_Code, [Year], [Month], [Day], and NB (new
business). Fire_NB has the same fields, just with different data.
The query... I'm making a query in which Agents is joined to both Auto_NB and
Fire_NB on the field Agent_Code. The joins are left joins, to capture all
the agents even if there was no production. I perform a simple grouped query:
SELECT Agents.Agent_Code, Sum(Auto_NB.NB) AS Auto_NB, Sum(Fire_NB.NB) AS
Fire_NB
FROM ... and the left join statements...
WHERE Auto_NB.[Year]=2008 AND Auto_NB.[Month]=3 AND Fire_NB.[Year]=2008 AND
Fire_NB.[Month]=3
GROUP BY Agents.Agent_Code;
The problem... Let's say Agents James Joyce sold 5 Auto policies on the first
of the month and 10 on the second, and that he sold 6 Fire policies on the
third and 12 on the fourth of the month. The dataset my query returns tells
me that Jim sold 30 Auto policies instead of 15 -- apparently using Cartesian
products. Yet I can't do any kind of join between the Auto and Fire tables
on their [Year] and [Month] fields, because it causes ambiguous outer joins.
The question: Is there an easy way around this? I've done so by using
subqueries for now, but they are unbearably slow.
question with general implications. I've simplified it, since the real code
is pretty gargantuan.
The database... The Access database I set up has a table called Agents,
another table called Auto_NB, and another one called Fire_NB. Auto_NB has
various fields, such as Agent_Code, [Year], [Month], [Day], and NB (new
business). Fire_NB has the same fields, just with different data.
The query... I'm making a query in which Agents is joined to both Auto_NB and
Fire_NB on the field Agent_Code. The joins are left joins, to capture all
the agents even if there was no production. I perform a simple grouped query:
SELECT Agents.Agent_Code, Sum(Auto_NB.NB) AS Auto_NB, Sum(Fire_NB.NB) AS
Fire_NB
FROM ... and the left join statements...
WHERE Auto_NB.[Year]=2008 AND Auto_NB.[Month]=3 AND Fire_NB.[Year]=2008 AND
Fire_NB.[Month]=3
GROUP BY Agents.Agent_Code;
The problem... Let's say Agents James Joyce sold 5 Auto policies on the first
of the month and 10 on the second, and that he sold 6 Fire policies on the
third and 12 on the fourth of the month. The dataset my query returns tells
me that Jim sold 30 Auto policies instead of 15 -- apparently using Cartesian
products. Yet I can't do any kind of join between the Auto and Fire tables
on their [Year] and [Month] fields, because it causes ambiguous outer joins.
The question: Is there an easy way around this? I've done so by using
subqueries for now, but they are unbearably slow.