Vexxums Vexxums - 2 months ago 6
MySQL Question

Query That Pulls Duplicate Parts On The Same Order

In my system, there should not be the same part# listed on an order more than once. I'm trying to write a query that will pull up any parts that appear on an order more than once. For example:

Order# Part QTY
1 A 1
1 A 1
1 B 5
2 A 4
2 B 4
2 C 3
3 A 5
3 B 5
3 B 7
4 A 3
4 B 6
5 A 3


So the problems here would be Order # 1 because part A appears more than once, and the same thing with Order# 3 because part B appears more than once. The rest of the orders would be fine. Where would I start if I want to achieve something like this.

Answer

The following query literally follows what you are asking. It aggregates by the combination of order and part, and returns those values when they occur more than once for a given pair.

SELECT Order, Part
FROM yourTable
GROUP BY Order, Part
HAVING COUNT(*) > 1