1

I've been banging my head against my keyboard trying to get this to work. Here's how my simple two column table (named "Test") looks:

part_id cust_id


1..........1

1..........1

2..........2

3..........3

I need to write a select statement that will list all cust_id that ordered the same part_id more than once and also lists the part_id. So far the closest I've been able to come is:

SELECT cust_id, COUNT(part_id)
FROM TEST
GROUP BY cust_id
HAVING COUNT(part_id) > 1;

Which only tells me how many customers ordered more that one part. The same part_id is the key here. Any tips would be greatly appreciated!

1
  • Given your sample table, shouldn't you be using cust_id instead of customer_id? Otherwise the query looks fine. Commented Feb 17, 2012 at 14:40

1 Answer 1

2

You're so close. Just include the part_id in both the SELECT and GROUP BY.

SELECT cust_id, part_id
FROM TEST
GROUP BY cust_id, part_id
HAVING COUNT(*) > 1;
Sign up to request clarification or add additional context in comments.

1 Comment

That was it! Thank you so much! This was driving me crazy all afternoon. You just saved my from a sleepless night :)

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.