I have a database representing multiple items per box. Each box is represented by a barcode, and each box also has a non-unique tag field. So a sample of the table might look like this:
| Item | Box_Barcode | Box_Tag | | I001 | Box0001 | TagA | | I002 | Box0001 | TagA | | I003 | Box0002 | TagB | | I004 | Box0002 | TagB | | I005 | Box0003 | TagC | | I006 | Box0003 | TagD | | I007 | Box0004 | TagE | | I008 | Box0004 | TagF |
What I'm trying to get is a query that only returns distinct combinations of Box_Barcode and Box_Tag where there is more than one Box_Tag per Box_Barcode. For the sample above, I would expect this:
| Box_Barcode | Box_Tag | | Box0003 | TagC | | Box0003 | TagD | | Box0004 | TagE | | Box0004 | TagF |
Box001 and Box002 are not returned because they each have only one Box_Tag associated with them.
I keep trying a variation of the following:
SELECT DISTINCT box_barcode, box_tag
FROM TableName
GROUP BY box_barcode, box_tag
HAVING COUNT(box_barcode) > 1
ORDER BY box_barcode
The variations I've tried include using "box_tag" and the asterisk in the HAVING clause. However, the results that I get list Box_Barcodes with singular Box_Tags. So, Box0001 and Box0002 from the sample above. However, the number of rows returned is much less than if I took the HAVING line out of the code. Obviously, I'm missing something.
I've seen this question asked before, but it has always involved one field in the SELECT statement, and I can't figure out the syntax for multiple fields.