Search code examples
sqlsql-servert-sqlaggregate-functionshaving-clause

How to extract duplicate orders from DB


Using SQL Server 2014. Taking the following recordset:

enter image description here

I need to find a way to extract the ordOrderNum which is duplicates, with a different DeliveryNum. In this example, orders 93400460 and 93400467 would be extract, because they are duplicates. Order 93408170 is ok. How can I do that??!

thanks for your time and help


Solution

  • You can use group by and having:

    select ordOrderNum 
    from mytable
    group by ordOrderNum
    having min(ordDeliveryNum) <> max(ordDeliveryNum)