Results 1 to 9 of 9
  1. #1
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108

    Query Producing Too Many Records

    My raw data contains a text string with various codes separated by an underscore. For example, my raw data would contain a string like this, "SAMPLE_DATA_ABC1_NYC", and each of those codes would have corresponding name that is mapped out on another table.



    I'd like to set up a query that would evaluate the raw data, look up the associated code (e.g. "_ABC1_") and then return the corresponding name. (See screenshot for an example.)

    Click image for larger version. 

Name:	Sample Data.png 
Views:	15 
Size:	4.3 KB 
ID:	15246

    Any idea how to do this? I tried using IIf and InStr statements, but I keep getting excess rows.


    Code:
    SELECT st.Campaign, st.Month, st.Week, st.Placement, st.[Placement ID], 
    IIf(InStr(st.placement, ic.[findInventoryCategory]) > 0,ic.[InventoryCat],"N/A") AS [Inventory Category], 
    IIf(InStr(st.placement, pth.[findPathway]) > 0,pth.[Pathways],"N/A") AS [Pathway Name]
    FROM RawDataFile AS st, mpInventoryCategory AS ic, mpPathway AS pth;

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The excess rows are almost certainly because you don't have a join between tables in the FROM clause. Without joins, you get a Cartesian product. The tables should be joined on whatever field(s) relate them.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Why is multiple discrete values combined into a single string to begin? That is poor data structure.

    I expect the main reason is there is no JOIN clause. This results in a Cartesian join - every record of each table joins to every record of each of the other tables.

    Also, RawDataFile probably has 1-to-many relationship with both mpInventoryCategory and mpPathway.

    Are the Product and City the only two values of interest? These correspond to the ABC1 and NYC components of the string? Can SAMPLE_DATA be discarded? Is the length of each component consistent? Will there always be 4 parts?
    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.

  4. #4
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108
    Thanks. Is there any way for me to join the tables? The only common element would be codes such as "_ABC1_", etc., which are found within the string in the RawDataFile.

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    One way is to create a query on your one table that has all the combined fields, and create a bunch of calculated fields to pull them out (using Text functions like INSTR, LEFT, RIGHT, MID, etc).

    Then, you can link that query to your other table based on the common join field(s).

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Breaking up 3-part data within query is not too difficult. See post 6 in https://www.accessforums.net/import-...ers-41262.html

    So unless the length of each part is the same for all records, more than 3 parts will probably need a custom function.
    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.

  7. #7
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    With the common elements embedded in the text string, and not in their own fields, then you can't join the tables in the normal way in design view - you will have to use a where clause in a criteria field, and to do that you would have to know that each part of the string field always starts in at the same character position, and is the same length.

    One of the where's might look something like this:

    where mid(rawdatafile.string_data,18,3) = pth.city (It is not at all clear from your example what your field names are)

    As June7 points out, having multiple data items encoded into the same field is not good database practice, and this is one reason why.

    HTH

    John

  8. #8
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108
    Quote Originally Posted by JoeM View Post
    One way is to create a query on your one table that has all the combined fields, and create a bunch of calculated fields to pull them out (using Text functions like INSTR, LEFT, RIGHT, MID, etc).

    Then, you can link that query to your other table based on the common join field(s).
    Thanks. So, I actually took that approach in Excel, but I decided to try Access since the formula calculations moved very slowly when I had a "large" dataset (i.e. thousands of rows).

    I was using Find and Substitute functions in Excel and finding the nth instance of the underscore. Is there a function that can do that in Access (find the nth instance), or does that have to be a custom function?

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    There is no intrinsic function in Access to do that. Excel functions can be referenced in VBA. Regardless, looks like have to write custom function. Invoking Excel functions within the custom function is an option.
    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.

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

Similar Threads

  1. Query Producing Blank Rows? Fault IIf Logic?
    By kestefon in forum Access
    Replies: 6
    Last Post: 12-18-2013, 06:13 PM
  2. HELP Subform is producing multiple records
    By VeganLiving in forum Forms
    Replies: 7
    Last Post: 08-21-2013, 12:00 PM
  3. Producing A Schedule
    By dylcon in forum Access
    Replies: 11
    Last Post: 06-14-2013, 01:36 PM
  4. Help producing queries
    By skidia in forum Access
    Replies: 1
    Last Post: 05-13-2013, 12:21 PM
  5. Replies: 15
    Last Post: 10-22-2012, 06:06 PM

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