0

I have a customer table, an item table and a transaction table with the following columns:

Customer - ID, Name

Item - ID, Description

Transaction - ID, CustID (Foreign key Customer(ID)), ItemID (Foreign Key Item(ID))

Using this query, can anybody help me create a query to answer the following question:

If a particular customer has participated in a transaction which involves a given ItemID (ie. the customer has bought a particular item), return a count of the total number of transactions that customer is involved in. The trick (and the part I cannot work out by myself) is how to include transactions in the count that do not involve the ItemID used in the query.

1
  • 3
    It is polite to show your effort towards the solution. Commented Jun 14, 2015 at 6:57

4 Answers 4

2

You can solve this problem in two steps:

  1. Write a query that returns the relevant customers' IDs. If you write that query as a subquery or a CTE (common table expression, i.e. a WITH clause), you don't even need to put the results in a temporary table.

  2. Join the resulting table from (1) to the transaction table (to filter out all transactions that you're not interested in), then group by customer ID (so that you can use aggregate functions in the SELECT clause), and select the COUNT(DISTINCT TransactionId).

Something along these lines:

WITH relevantCustomers (CustomerId) AS
(
    SELECT DISTINCT CustomerId
    FROM Transactions
    WHERE ItemId = 123
)
SELECT t.CustomerId, COUNT(DISTINCT t.TransactionId)
FROM Transactions t
INNER JOIN relevantCustomers rc ON t.CustomerId = rc.CustomerId
GROUP BY t.CustomerId

This gives you a set of all customers (by their ID) that bought item 123, along with the total number of transactions for each of these.

If you are interested in only one specific customer, you can add a WHERE clause to the "outer" query that filters by that customer's ID.

Sign up to request clarification or add additional context in comments.

Comments

0
select count(*) from Transaction where CustID in (select CustID from Transaction where CustID = 123 and ItemID = 456)

If customer 123 hasn't been involved in a transaction for item 456, then the query will return a count of 0 because the subquery had no custid entries in it.

3 Comments

Hi Jim, That is close to the answer but I am looking for a way to achieve waht you have without specifying a custID in the nested select statement.
In that case, I think stakx's answer is what you're looking for.
@MarkD, what's the reason you want to avoid the nested select statement?
0

Thank you Jim Reminding me about the WHERE something is IN (NESTED QUERY) got me on the right track.

I have come up with the following answer to my problem:

SELECT Name, Count(Name)
FROM customer c
JOIN transaction t
ON c.ID = t.CustID
WHERE CustID IN (SELECT CustID 
FROM transaction
WHERE ItemID = 2);

Apologies to Stakx, I was looking for an answer at the same time you were obviously providing one.

1 Comment

I might be mistaken, but I suspect this will only work when an item can be bought by at most one customer. If several customers can buy the same item, the COUNT(…) will not distinguish between them. Please be aware of that. In either case, I'm glad if you found a solution that works for you!
0

For "If a particular customer has participated in a transaction which involves a given ItemID" - You mean you have the customer ID and product ID ?If that is the case you can just write -

SELECT * FROM TRANSACTION 
where CUSTID={"your customer id"} 
and ItemID = {"Required Item ID"} ;

For second part you can try something like this :

Select count(*) from TRANSACTION 
where CUSTID={"your customer id"} 
and ItemID = {"Required Item ID"} ;

You can play around SQL queries here to understand SQL better .

Regards Paritosh

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.