Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a Northwind example based on the Orders table.</p> <p>There are duplicates based on the (EmployeeID , ShipCity , ShipCountry) columns.</p> <p>If you only execute the code between these 2 lines:</p> <pre><code>/* Run everything below this line to show crux of the fix */ /* Run everything above this line to show crux of the fix */ </code></pre> <p>you'll see how it works. Basically:</p> <p>(1) You run a GROUP BY on the 3 columns of interest. <strong>(derived1Duplicates)</strong></p> <p>(2) Then you join back to the table using these 3 columns. <strong>(on ords.EmployeeID = derived1Duplicates.EmployeeID and ords.ShipCity = derived1Duplicates.ShipCity and ords.ShipCountry = derived1Duplicates.ShipCountry)</strong></p> <p>(3) Then for each group, you tag them with Cardinal numbers (1,2,3,4,etc) <strong>(using ROW_NUMBER())</strong></p> <p>(4) Then you keep the row in each group that has the cardinal number of "1". <strong>(where derived2DuplicatedEliminated.RowIDByGroupBy = 1)</strong></p> <pre><code> Use Northwind GO declare @DestinationVariableTable table ( NotNeededButForFunRowIDByGroupBy int not null , NotNeededButForFunDuplicateCount int not null , [OrderID] [int] NOT NULL, [CustomerID] [nchar](5) NULL, [EmployeeID] [int] NULL, [OrderDate] [datetime] NULL, [RequiredDate] [datetime] NULL, [ShippedDate] [datetime] NULL, [ShipVia] [int] NULL, [Freight] [money] NULL, [ShipName] [nvarchar](40) NULL, [ShipAddress] [nvarchar](60) NULL, [ShipCity] [nvarchar](15) NULL, [ShipRegion] [nvarchar](15) NULL, [ShipPostalCode] [nvarchar](10) NULL, [ShipCountry] [nvarchar](15) NULL ) INSERT INTO @DestinationVariableTable (NotNeededButForFunRowIDByGroupBy , NotNeededButForFunDuplicateCount , OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry ) Select RowIDByGroupBy , MyDuplicateCount , OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry From ( /* Run everything below this line to show crux of the fix */ Select RowIDByGroupBy = ROW_NUMBER() OVER(PARTITION BY ords.EmployeeID , ords.ShipCity , ords.ShipCountry ORDER BY ords.OrderID ) , derived1Duplicates.MyDuplicateCount , ords.* from [dbo].[Orders] ords join ( select EmployeeID , ShipCity , ShipCountry , COUNT(*) as MyDuplicateCount from [dbo].[Orders] GROUP BY EmployeeID , ShipCity , ShipCountry /*HAVING COUNT(*) &gt; 1*/ ) as derived1Duplicates on ords.EmployeeID = derived1Duplicates.EmployeeID and ords.ShipCity = derived1Duplicates.ShipCity and ords.ShipCountry = derived1Duplicates.ShipCountry /* Run everything above this line to show crux of the fix */ ) as derived2DuplicatedEliminated where derived2DuplicatedEliminated.RowIDByGroupBy = 1 select * from @DestinationVariableTable </code></pre> <p><em>emphasized text</em>*emphasized text*<em>emphasized text</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload