Query for Lowest 2nd lowest and 3rd lowest value

mpb.vu2

Registered User.
Local time
Today, 12:05
Joined
May 31, 2005
Messages
41
Hello there...

I have a data set as follows:

Product CompanyA CompanyB CompanyC .... CompanyZ

aaa 2.3 2.4 2.5
bbb 3.4 - 3.1

and so on...

The value represents the price of products...

I want to make a query that will show me results in following heads..


Product MinimumPrice CompanyName
aaa 2.3 CompanyA
bbb 3.1 CompanyC



How can I proceed??

Looking for some help

Regards,
 
What is your table structure like? It sounds like your table is as follows;

tblData
Product (String)
COmpanyA (Number) - Company A's price
COmpanyB (Number) - Company B's price
COmpanyC (Number) - Company C's price

If so, you can use three queries.

qryPriceData
Code:
SELECT Product, CompanyA as Price, "CompanyA" as Company FROM tblData;

UNION SELECT Product, CompanyB as Price, "CompanyB" as Company FROM tblData;

UNION SELECT Product, CompanyC as Price, "CompanyC" as Company FROM tblData;

qryPriceMin
Code:
SELECT Product, Min(Price) AS MinOfPrice 
FROM quniPriceData
GROUP BY Product;

qryCompanyCheapest
Code:
SELECT qryPriceMin.Product, qryPriceMin.MinOfPrice, quniPriceData.Company
FROM qryPriceMin INNER JOIN quniPriceData 
ON (qryPriceMin.MinOfPrice = quniPriceData.Price) 
AND (qryPriceMin.Product = quniPriceData.Product);

HOwever, I think you it would be better to have three tables instead of one.

tblProduct
ProductID (Autonumber - Primary Key)
strProduct (String)

tblCompany
CompanyID (Autonumber - Primary Key)
strCompany (String)

tblPrice
PriceID (Autonumber - Primary Key)
CompanyID (Foreign Key)
ProductID (Foreign Key)
curPrice (Currency)

Also, create a unique multi-field index using COmpanyID and ProductID. This wil make it so that you can't have a multiple company-product combinations (One price for each unique Company-Product). Alternatively you can use the unique multi-field index as your primary key instead of an Autonumber.

This would allow you to have two queries instead.

qryPriceMinV2
Code:
SELECT tblPrice.ProductID, strProduct, Min(curPrice) AS MinOfcurPrice
FROM tblProduct 
INNER JOIN tblPrice 
ON tblProduct.ProductID = tblPrice.ProductID
GROUP BY tblPrice.ProductID, tblProduct.strProduct;

qryCompanyCheapestV2
Code:
SELECT qryPriceMinV2.strProduct, qryPriceMinV2.MinOfcurPrice, tblCompany.strCompany
FROM qryPriceMinV2 
INNER JOIN (tblCompany INNER JOIN tblPrice ON tblCompany.CompanyID = tblPrice.CompanyID) 
ON (qryPriceMinV2.MinOfcurPrice = tblPrice.curPrice) 
AND (qryPriceMinV2.ProductID = tblPrice.ProductID);

But I am assuming that you are looking for the minimum price. However, your thread title is "Query for Lowest 2nd lowest and 3rd lowest value" which doesn't seem to match the query result example that you provided.
:confused:

At any rate, I am attaching an Access 97 file with examples of what I have discuss. If it isn't what you are looking for, please post back and I will try to help as my time and skill permit!
:)
 

Attachments

Last edited:

Users who are viewing this thread

Back
Top Bottom