I am trying to find duplicate data across 3 columns in the same table. I am looking for any orders which have the same ReferenceNumber, PremiseId & DatePlaced.
For example an order with ReferenceNumber of 12345 from PremiseId 67890 with DatePlaced 02/08/17 would show up, because all three criteria are satisfied.
This is what I have so far, which was taken from another answer on this site.
SELECT ReferenceNumber, PremiseId, DatePlaced
FROM [ypolive_Integration].[dbo].[OrderHeaders]
WHERE ReferenceNumber in
(SELECT ReferenceNumber FROM [ypolive_Integration].[dbo].[OrderHeaders]
GROUP BY ReferenceNumber
HAVING COUNT(*)>1)
Order By DatePlaced desc
This is returning results for ReferenceNumber, PremiseId & DatePlaced. However this is showing all data and I only want it to display the duplicates which match all three criteria.
I am very much a novice at this sort of thing, so apologies if this is completely wrong!
This is now solved with the below code:
select ReferenceNumber, PremiseId , CONVERT(VARCHAR(10),DatePlaced, 112) as
DatePlaced,count(1) as cnt
from [ypolive_Integration].[dbo].[OrderHeaders]
group by ReferenceNumber, PremiseId , CONVERT(VARCHAR(10),DatePlaced, 112)
having count(1) > 1
order by DatePlaced desc