Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query with strange criteria 1

Status
Not open for further replies.

bernie10

Technical User
Sep 13, 2004
219
US
Hi everyone,

Just wanted to know if anyone had any thoughts on this.

Suppose I have a table as follows:

Supplier Price Valid
Jim’s 24.32 Y
Bill’s 22.11 N
Joe’s 26.01 N
Joe’s 29.04 Y
Bill’s 24.12 N
Jim’s 25.12 Y

I would like to get a record with the lowest price for each supplier. But, I only want valid records to be considered. So this is done easily enough, I simply make a query where Valid = “Y” and then make another query based off that one to find the min price grouping by manufacturer.

Here’s the problem- I have been asked to add the following piece. IF the supplier has no valid records, then I need to include his lowest priced item on the list, even though the record is marked as not being valid... this is ONLY if the supplier has no valid records. So for the above table the output should be as follows:

Supplier Price Valid
Jim’s 24.32 Y
Bill’s 22.11 N
Joe’s 29.04 Y

Notice since Bill did not have a valid record we picked one his invalid record’s with the lowest price. Can this be done? I can’t think of any way, but I thought maybe someone out there might have some ideas.

Thanks in advance for any help,
Collen
 
try something like

select supplier,min(price)
from tblname
group by supplier
 
Maybe something like
Code:
(select supplier,min(price)
from tblname
Where Valid = 'Y'
group by supplier)

UNION 

(select supplier,min(price)
from tblname
Where Valid = 'N' AND Supplier NOT IN 
(Select DISTINCT Supplier From tblname Where Valid = 'Y')
group by supplier)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top