Results 1 to 4 of 4
  1. #1
    MarcieFess is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    9

    Query not returning expected results

    Background: I have a table that doesn't have unique records but it should. The only way to make the records unique is to combine 2 fields in an index. I've done that, but it won't let me make the index Unique because it says I have duplicate records. I don't see any.

    Before modifying the table with the 2-column index, I had created this query:

    Code:
    SELECT tblStoreProducts.UPC, tblStoreProducts.ProductKey, Product.ProductName, 
    tblStoreProducts.StoreProductKey, tblStoreProducts.StoreKey
    FROM Product INNER JOIN tblStoreProducts
         ON Product.ProductKey = tblStoreProducts.ProductKey
    WHERE (((tblStoreProducts.UPC) In (SELECT [UPC] FROM [tblStoreProducts] As Tmp 
    
    GROUP BY [UPC] HAVING Count(*)>1 )))
    ORDER BY tblStoreProducts.UPC;
    Running the query, I just don't see the duplicates.

    Someone has suggested that I need to somehow change the Group By to group by the fields that will be grouped in the index (StoreKey and ProductKey). I have no idea how to do that.

    Here is the issue. I have a table (Product) that contains 1 unique entry for every UPC. Information unique to that product is stored here. The UPC is unique, and ProductKey (an Autonumber field) is the primary key

    I have another table (tblStoreProducts) that keeps track of which products are in a particular store. UPC is not unique, as it will occur in this table as many times as there are stores that carry that UPC. Likewise, ProductKey is not unique, for the same reason.

    StoreKey is the store identifier. It, again, is not unique in this table as one store will have hundreds of product and thus an entry in that store for each product.



    I created an 2-field Index on StoreKey and ProductKey but Access says I have duplicates so it won't let me make that a unique index.

    How do I tweak the above query to find the duplicates? It appears to me that it's just showing me each store that a particular UPC exists in.

    The only reason I have it linked to the Product table is so that I can pull in the product name to the results so I know what products we are dealing with.

    Any suggestions?

    Marcie

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    First, I think you may have a misunderstanding of what a unique key is used for.
    There's nothing wrong with tblStoreProducts having duplicates, for example, of StoreKey. In that table, StoreKey is a data field. If you feel you need a unique key in that table, then add an autokey field. I generally do, but it's not crucial. If you feel you need a unique index, then you can create a dual-field index field, but I generally don't do that. I would, instead, always read for existence before I tried to add or subtract. But, that's a style thing - read before, or error-trap after.
    Here's an approach to solve your right-now problem.
    1) Back up your database and do this in a test copy.
    2) Create a second table with the same structure.
    3) Add the unique dual-field index.
    4) Insert the old table data into the new table. Some records will error off.
    5) Update the new table with the SUM of the old table inventory records.
    6) Replace the old table data with the new table data.
    7) Add the unique dual-field index to the old table.

  3. #3
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    This will find which Products are duplicated at each store. You'll have to add in code to get the store name, if you want it.
    Code:
    SELECT
       TP.ProductName,
       TS.ProductKey,
       TS.StoreKey,
       TS.UPC,
       TS.DupCount
    FROM  
       Product AS TP
       INNER JOIN
         (SELECT [StoreKey], [UPC], First([ProductKey]) AS ProductKey, Count([ProductKey]) As DupCount    
          FROM [tblStoreProducts]
          GROUP BY [StoreKey], [UPC] 
          HAVING Count(*)>1) AS TS
       ON TS.ProductKey = TP.ProductKey;

  4. #4
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Got your message. Glad it worked. Closing thread.

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

Similar Threads

  1. Replies: 3
    Last Post: 07-23-2013, 01:30 PM
  2. Query not returning all expected results
    By amenitytrust in forum Queries
    Replies: 6
    Last Post: 11-05-2012, 07:13 AM
  3. Replies: 13
    Last Post: 01-13-2011, 10:15 AM
  4. Query Bug? Not returning consistent results
    By trb5016 in forum Queries
    Replies: 4
    Last Post: 06-15-2010, 12:00 PM
  5. Query not returning all of the results
    By velvettiger in forum Queries
    Replies: 4
    Last Post: 03-11-2010, 06:56 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