Results 1 to 8 of 8
  1. #1
    mcchung52 is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2012
    Posts
    15

    Query to pick out a row with lowest cost with latest date

    Hi all,
    I need to program a query that tries to pull the cheapest cost w/ date from a table of drugs
    (has link_code, drugID, drugName, cost, dateOfPurchase .... yea it's a linear table for now).

    Here's the twist: rows or drugs deemed to be similar will have the same link_code number. No link number just means


    a similar drug hasn't been entered into the database.

    So I will have another query with drugIDs that I need to combine with the results of this query:

    If the drugID is found in the table then it'll see if there's a similar drug by checking out the link_code and
    will look through all the rows grouped by that link_code to pick out the latest AND cheapest drug purchased.
    Then the query will output drugID, drugname, cost, and dop(dateOfPurchase).
    If the drugID is not found don't do anything.

    I hope this is clear.
    Thanks.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Need to work with data. Want to provide db for analysis? Follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    mcchung52 is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2012
    Posts
    15
    here is the attachment.
    i only have 5 products as a sample.
    sorry for getting back so late.
    Attached Files Attached Files

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    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.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    mcchung52 is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2012
    Posts
    15
    yes it is the link code.
    and i know it doesn't make sense because i'm gonna join this query with another query that has the same drugID.
    all advices taken and greatly appreciated.
    i'll try the query...
    thanks.

    EDIT: Wow that query takes mighty long time.


    Quote Originally Posted by June7 View Post
    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.

  6. #6
    mcchung52 is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2012
    Posts
    15
    so what does that query do? exactly?

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    It returns the lowest cost for each drug for the latest date associated with each drug in the table. Isn't that what you wanted?

    Now you can build a form where user enters criteria for drug to search for if that is what you were intending by 'If the drugID is found in the table'.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    mcchung52 is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2012
    Posts
    15
    oh wow ok. thank you. let me play with this and get back to you.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Using expression builder to find lowest date
    By MMcKenna in forum Queries
    Replies: 1
    Last Post: 03-20-2012, 02:28 PM
  2. How do you get the latest date?
    By radicrains in forum Queries
    Replies: 6
    Last Post: 10-27-2010, 08:59 PM
  3. Query for latest date? what am I doing wrong?
    By Overdive in forum Queries
    Replies: 1
    Last Post: 12-18-2009, 06:04 AM
  4. Query to find latest date
    By Lockrin in forum Access
    Replies: 2
    Last Post: 12-16-2009, 10:00 AM
  5. Query to find lowest value
    By AccessNubie in forum Access
    Replies: 3
    Last Post: 11-30-2009, 07:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums