Check for Duplicate - Example



WITH CTE AS(
   SELECT [col1], [col2], [col3], [col4], [col5], [col6], [col7],
       RN = ROW_NUMBER()OVER(PARTITION BY col1 ORDER BY col1)
   FROM dbo.Table1
)
DELETE FROM CTE WHERE RN > 1
http://stackoverflow.com/questions/18390574/how-to-delete-duplicate-rows-in-sql-server

This example determines duplicates by a single column col1 because of the PARTITION BY col1. If you want to include multiple columns simply add them to the PARTITION BY: ROW_NUMBER()OVER(PARTITION BY Col1, Col2, ... ORDER BY OrderColumn)




WITH CTE AS ( SELECT *,RN=ROW_NUMBER() OVER (PARTITION BY orgName ORDER BY orgName DESC) FROM organizations ) select * from CTE where RN>1 go
WITH CTE (Code, n, DuplicateCount) AS ( SELECT Code, n, ROW_NUMBER() OVER(PARTITION BY Code, n ORDER BY Code) AS DuplicateCount FROM dbo.TestTable ) SELECT * FROM CTE ORDER BY Code, n, DuplicateCount GO



USE [SOPHOS521];
WITH numbered AS
( SELECT FirstDetectedAt,EventID,ROW_NUMBER() OVER (PARTITION BY FirstDetectedAt ORDER BY EventID DESC) AS rownum
FROM
      dbo.AllComputersInSubestate(1) c
      INNER JOIN [Sophos Reporting Interface].[ComputerHostData]('en', null, null) sric           ON c.ID = sric.ComputerID
      INNER JOIN [Sophos Reporting Interface].[ThreatInstances]('en', null, null) t ON           sric.ComputerID = t.ComputerID
      INNER JOIN [Sophos Reporting Interface].[ThreatEventData]('en', '1900-01-01                 00:00:00', '9999-12-31 23:59:59') te ON t.ThreatID = te.ThreatID

) SELECT StatusName,ActionTakenName ,*
FROM numbered a,
      dbo.AllComputersInSubestate(1) c
      INNER JOIN [Sophos Reporting Interface].[ComputerHostData]('en', null, null) sric           ON c.ID = sric.ComputerID
      INNER JOIN [Sophos Reporting Interface].[ThreatInstances]('en', null, null) t ON           sric.ComputerID = t.ComputerID
      INNER JOIN [Sophos Reporting Interface].[ThreatEventData]('en', '1900-01-01                 00:00:00', '9999-12-31 23:59:59') te ON t.ThreatID = te.ThreatID
   WHERE a.rownum=1 and t.FirstDetectedAt=a.FirstDetectedAt And a.EventID=te.EventID --
   ORDER BY a.FirstDetectedAt DESC

Select rows that occur only once

SELECT Number, MIN(c1) AS c1, MIN(c2) AS c2, MIN(c3) AS c3

FROM [table]

GROUP BY Number

HAVING COUNT(*) = 1

https://stackoverflow.com/questions/40682070/select-only-those-rows-which-occur-exactly-once