"bronson"wrote
Thanks for your help. I'm currently using two queries. This works fine.
However, I have begun to clean up my database for 2 main reasons:
First, because it's a bit clogged up with as you would put it,
"divide-and
conquer" queries
Second, because I would like to learn and understand new techniques
(subqueries in particular)
Your SQL is fine but it's approximately my first query. My second query
then
sums up every position based on ProdID only (There's no need for
sorting/grouping by ModID anymore). Do you have a suggestion how to
perform
the whole calculation in one single step? My guess is to embed your SQL
in an
even lager query that doesnt group by ModID
q1:
SELECT
t.ProdID,
t.ModID,
Max(t.InvDate) As LatestDate
FROM yurtable As t
Group By
t.ProdID,
t.ModID;
q2:
SELECT
t.ProdID,
t.ModID,
t.InvDate As LatestDate,
t.Inv
FROM
yurtable As t
INNER JOIN
q1
ON
t.ProdID = q1.ProdID
AND
t.ModID = q1.ModID
AND
t.InvDate = q1.LatestDate;
then sum q2.Inv over q2.ProdID
group in final query.....
If your field names are named such that
they do not need bracketed, you could
replace "q1" in FROM clause of q2 with
actual SQL of q1. Just put brackets
around the SQL, end with period and
give it an alias of "q1"
SELECT
t.ProdID,
t.ModID,
t.InvDate As LatestDate,
t.Inv
FROM
yurtable As t
INNER JOIN
[SELECT
t.ProdID,
t.ModID,
Max(t.InvDate) As LatestDate
FROM yurtable As t
Group By
t.ProdID,
t.ModID]. As q1
ON
t.ProdID = q1.ProdID
AND
t.ModID = q1.ModID
AND
t.InvDate = q1.LatestDate;
This bracket thing is the killer.
The query parser will not allow any
*further brackets* within a subquery
used in the FROM clause.
It might be tempting to replace "q2"
in your summing query with SQL above
but Access will choke because we
already "have used up our brackets"
when we put them around q1.
Why not just use parentheses instead of
brackets? For example
SELECT
q2.ProdID,
q2.ModID,
q2.LatestDate,
SUM(q2.Inv) As InvSum
FROM
(SELECT
t.ProdID,
t.ModID,
t.InvDate As LatestDate,
t.Inv
FROM
yurtable As t
INNER JOIN
(SELECT
t.ProdID,
t.ModID,
Max(t.InvDate) As LatestDate
FROM yurtable As t
Group By
t.ProdID,
t.ModID) As q1
ON
t.ProdID = q1.ProdID
AND
t.ModID = q1.ModID
AND
t.InvDate = q1.LatestDate) As q2
GROUP BY
q2.ProdID,
q2.ModID,
q2.LatestDate;
which will "work" but when Access
gets around to saving the query, it will
try to change the parentheses to brackets
with a period and choke.
You see posts asking for help all the time
with this scenario, "it worked, but won't save"
or some such wording.
If your field names do not need brackets,
you get one level of subquery as table in
your big query.
We can go back to qryLatestOfProdModGroup
SELECT
m.InvDate,
m.ProdID,
m.ModID,
m.Inv
FROM yurtable As m
WHERE
m.InvDate =
(SELECT Max(t.InvDate)
FROM yurtable As t
WHERE
t.ProdID = m.ProdID
AND
t.ModID = m.ModID);
which found our latest date through a correlated
subquery. We can use that SQL as a "table" q2
in our summing query:
SELECT
q2.ProdID,
q2.ModID,
q2.LatestDate,
SUM(q2.Inv) As InvSum
FROM
[SELECT
m.InvDate As LatestDate,
m.ProdID,
m.ModID,
m.Inv
FROM yurtable As m
WHERE
m.InvDate =
(SELECT Max(t.InvDate)
FROM yurtable As t
WHERE
t.ProdID = m.ProdID
AND
t.ModID = m.ModID)]. As q2
GROUP BY
q2.ProdID,
q2.ModID,
q2.LatestDate;
hopefully the actual name of "yurtable"
did not require it to be bracketed, or
the bracket-within-a-bracket gotcha
will raise its ugly head again.
Did that help?
You can "design" with parentheses instead
of brackets, but just remember that Access
will try to change when query is saved....