Top 3 by Sum of Value

O

Opal

I am trying to create a query in Access 2003
that will give me the top 3 scrap parts by
value and I am getting stumped. I am trying
to build it as a sub-query....

SELECT ScrapData.Value, ScrapData.PartNo, ScrapData.Description
ScrapData.Date
FROM ScrapData
WHERE ScrapData.Value IN
(SELECT TOP 3 Sum(ScrapData.Value) AS SumOfValue
FROM ScrapData as Dupe
WHERE Dupe.PartNo = ScrapData.PartNo);

and I am getting 55 random records, not my top 3 by
sum....can someone please point me in the right direction?
 
J

John Spencer

Well you seem to be getting records where the SUM of the Value is equal to the
Value of the part no. That would indicate you are getting records where there
is only one record for PartNo.

If you want all the records for the top 3 parts by value.

SELECT ScrapData.Value
, ScrapData.PartNo
, ScrapData.Description
, ScrapData.Date
FROM ScrapData
WHERE ScrapData.PartNo IN
(SELECT TOP 3 Dupe.PartNo
FROM ScrapData as Dupe
GROUP BY PartNO
ORDER BY Sum(Value) Desc)

If you want all records for the most expensive parts

SELECT ScrapData.Value
, ScrapData.PartNo
, ScrapData.Description
, ScrapData.Date
FROM ScrapData
WHERE ScrapData.PartNo IN
(SELECT TOP 3 Dupe.PartNo
FROM ScrapData as Dupe
ORDER BY Value Desc)

If you want something else try to describe what you do want in more detail.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
O

Opal

Hi John,

This is what I tried earlier:

SELECT TOP 3 ScrapData.PartNo, ScrapData.Description, Sum
(ScrapData.Value) AS SumOfValue, ScrapData.ScrapDate
FROM ScrapData
GROUP BY ScrapData.PartNo, ScrapData.Description, ScrapData.ScrapDate
HAVING (((ScrapData.ScrapDate) Between [Forms]![frmWeeklyrpt]!
[FromDate] And [Forms]![frmWeeklyrpt]![ToDate]))
ORDER BY Sum(ScrapData.Value) DESC;

But when I queried last week, I got 5 records returned:

PartNo. Desc Value Date
1 ABC $180 11/25
2 XYZ $125 11/24
3 JKL $60 11/23
1 ABC $60 11/26
1 ABC $60 11/24

I want it to sum all of PartNo. 1 from 11/24, 11/25 and 11/26 for a
total of $300

What you suggested, above, doesn't get me there.... :-(
 
O

Opal

So this is what I did. I created one query to collect
all scrap parts by date range selected:

SELECT ScrapData.ScrapDate, ScrapData.PartNo, ScrapData.Description,
ScrapData.CostCtr, ScrapData.Value
FROM ScrapData
WHERE (((ScrapData.ScrapDate) Between [Forms]![frmWeeklyrpt]!
[FromDate] And [Forms]![frmWeeklyrpt]![ToDate]));


took that query (qryPart1)

And created the following to give my top 3:

SELECT TOP 3 qryPart1.PartNo, qryPart1.Description, Sum
(qryPart1.Value) AS SumOfValue
FROM qryPart1
GROUP BY qryPart1.PartNo, qryPart1.Description
ORDER BY Sum(qryPart1.Value) DESC;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top