Originally Posted by
June7
Is the field [code] the link_code?
There is only one table in this db. Your requirement to determine 'If the drugID is found in the table' doesn't make sense unless the intent is to prompt user for a drugID (ndc ?) and search table for this parameter and return lowest cost.
However, consider this query:
SELECT ID, ndc, [drug name], [item#], [date], cost FROM gen WHERE gen.ID IN (SELECT TOP 1 ID FROM gen As Dupe WHERE Dupe.ndc=gen.ndc ORDER BY Dupe.ndc, Dupe.[date] DESC, Dupe.cost);
BTW, advise not to use spaces, special characters, punctuation (underscore is exception) in names, nor reserved words (such as 'date') as names.