0

I've searched the internet for a solution and racked my brains trying to figure this out, hopefully someone can help. Here is the basic situation.

I have a list of transactions in a table. Each transaction has fields for Client ID, A store ID, the product they bought, and a billing code (which is either 2, 3, 4, 5, 6, 7)

Some stores aren't using billing code 2 at all and I want to investigate this further.

I created a list of stores that don't use billing code 2. There was 20 stores.

I then created a query that gave me a list of clients associated with those 20 stores.

-------------------This is where I am now-----------

Basically I want to see if people if they go to Store X and buy Product Y are they treated differently (billing code) in two different stores. There is more money for the store if they don't use billing code 2 so their is an incentive not to use this code.

Is there a way to do a query and tell Access to give me clients that are using these 20 stores and another store (not of the 20) and do they purchase the same product elsewhere?

Any help will be appreciated. I have a feeling that I need to simplify this and take in multiple steps but I can't seem to work out a path from point A to point B on this. Thanks.

2
  • can you show us your codes ? Commented May 6, 2013 at 6:48
  • and also your table(s). It's likely all you need are a couple or queries and maybe a simple join. Commented May 6, 2013 at 11:03

2 Answers 2

1

Okay, so it sounds like you've got a query to find the stores that don't use billingCode=2

SELECT DISTINCT storeID
FROM tblPurchases
WHERE storeID NOT IN (SELECT storeID FROM tblPurchases WHERE billingCode=2);

I've saved that query in Access as [qryStoresNotUsing2] so I can use it below.

It also sounds like you've leveraged that first query to give you a list of clients who shop at those stores, which could be done with something like

SELECT DISTINCT clientID
FROM tblPurchases
WHERE storeID IN (SELECT storeID FROM qryStoresNotUsing2);

Now it sounds like you want to get some detail on clients who buy a product at one of the "non-2" stores and also buy the same product elsewhere.

We can start by generating a list of all the purchases where the same client buys the same product at different stores:

SELECT DISTINCT p1.clientID, p1.productID, 
        p1.storeID AS store1, p1.billingCode AS billingCode1, 
        p2.storeID AS store2, p2.billingCode AS billingCode2
FROM tblPurchases p1 INNER JOIN tblPurchases p2 
    ON p1.clientID=p2.clientID AND p1.productID=p2.productID AND p1.storeID<>p2.storeID

Note that the first two conditions in the ON clause ensure that the client and the product are the same, and the third condition ensures that the store is different.

Also note that a "not equals" self-join will produce multiple symmetrical results: You'll get a row for "Store_X, Store_Y" and another row for "Store_Y, Store_X". That okay, because now what we want to do is restrict those results to purchases where one of the stores is in the "non-2" list, so we'll just take the ones where there's a match on one side of the join (p1) by simply adding a WHERE clause to the query above:

SELECT DISTINCT p1.clientID, p1.productID, 
        p1.storeID AS store1, p1.billingCode AS billingCode1, 
        p2.storeID AS store2, p2.billingCode AS billingCode2
FROM tblPurchases p1 INNER JOIN tblPurchases p2 
    ON p1.clientID=p2.clientID AND p1.productID=p2.productID AND p1.storeID<>p2.storeID
WHERE p1.storeID IN (SELECT storeID FROM qryStoresNotUsing2);

That query will list the individual purchases. If you want just the clients you can omit the productID, storeID, and billingCode columns from the output and just do

SELECT DISTINCT p1.clientID FROM ...
Sign up to request clarification or add additional context in comments.

Comments

0

I would approach the problem by

1 - use a query to get a list of all products that have been billed by any store with billing code 2 - just the product codes, no other information

2 - link the transaction table to the query in step 1 and list the product, customer, store and billing code - probably in a crosstab query with product, customer and store down the side and billing code across the top

3 - visually scan this output to see the stores that don't use billing code 2 - this should answer your question or tell you if more investigation is required

Alternatively, you could use a select query in step 2 and add a condition for billing code <> 2 - since you started with all products billed with code 2 anywhere, this query will tell you (store, product, customer) for those stores that didn't use code 2 when another store did use code 2 for the same product

Another alternative, if this has to be customer specific - in query 1, get the customer/product combinations for all code 2 bill codes. Then link this to the transaction table on both customer and product, with a condition of bill code <> 2. This is the same logic as above - but will list (customer, product) combinations billed as code 2 in one store and some other bill code in another store

All of these are simple select or crosstab queries with a join from the 1st query to the 2nd.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.