Skip to content
Advertisement

Show when order number is unique for the item number

enter image description here

I attached a simple table with two columns, Item number and Order number. In the example attached only item number C5664 and A9930 met the criteria. How do I write this code in SQL?

Advertisement

Answer

If you group by the item numbers then the unique count for the order numbers will be 1

SELECT "Item Number", MAX("Order Number") AS "Order Number"
FROM yourtable AS t
GROUP BY "Item Number"
HAVING COUNT(DISTINCT "Order Number") = 1
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement