We help IT Professionals succeed at work.

Find minimum cost for records

intsup
intsup asked
on
269 Views
Last Modified: 2012-05-11
i have the following table called tblPNPrice and am looking for the lowest cost for the PN_id

tblPNPrice
idn             PN_id           Cost
1               123               $1.20
2               154               $7.23
3               123               $2.15
4               278               $11.78
5               189               $63.75
6               278               $15.11

so that the result would be:

qryPNPrice_MinCost
PN_id          MinCost
123               $1.20
154               $7.23
278               $11.78
189               $63.75


thank you!
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
Do a group by Query

SELECT blPNPrice.PN_id, Min(blPNPrice.Cost) AS MinOff2
FROM blPNPrice
GROUP BY blPNPrice.fPN_id;

Author

Commented:
matthews was the simplest to follow and worked right off the bat...

quick followup question...

is it possible to get a result of:

qryPNPrice_MinCost
idn       PN_id          MinCost
1      123               $1.20
2      154               $7.23
4      278               $11.78
5      189               $63.75

when i try to include the idn in the query it messes up the query and ALL records are shown instead of just the minimum cost for PN_id
CERTIFIED EXPERT
Top Expert 2010

Commented:
SELECT t1.idn, t1.PN_id, t1.Cost
FROM tblPNPrice t1 INNER JOIN
    (SELECT t2.PN_id, Min(t2.Cost) AS MinCost
    FROM tblPNPrice t2
    GROUP BY t2.PN_id) AS x ON t1.PN_id = x.PN_id And t1.Cost = x.MinCost
ORDER BY t1.idn, t1.PN_id

Open in new window

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.