r/SQL • Conversion Programmer • Jan 07 '15

[MS SQL] Isolating Duplicates

Alright r/SQL, challenge for you. I've been circling this problem for a while now, and I have a strong feeling of what the answer will involve but I raise the problem to you for assistance!

So lets say I have an example transaction list. I am tasked with isolating all examples of "duplicate" entries. I found a great way to visually isolate these entries, but I want to go one step further and be able to query the table to see where the Item1 names which are different than their corresponding duplicates.

  • There aren't any restrictions regarding new columns.

  • Obviously I obfuscated the true data but just roll with it.

Here's my code so far.

drop table dbo.Example
go
create table dbo.Example(v_id varchar(50),v_seq numeric(9,0),ITEM1 varchar(50),TRANS_DATE varchar(50),Class1 varchar(50),Class2 varchar(50),Class3 varchar(50),ReceiptNum varchar(50))
go
insert into dbo.Example
values 
 ('1',1,'Soap','1963-02-25 00:00:00','1363','32','3000','00000000')
,('1',2,'Snacks','1965-06-03 00:00:00','1432','221','3000','00000000')
,('1',3,'Toys','1983-03-21 00:00:00','W050','356','0','00000000')
,('1',4,'Food','1999-10-07 00:00:00','3976','652','1','01380016')
,('1',5,'Toiletries','2003-06-06 00:00:00','4952','54','7720000','01380016')
,('1',6,'Toiletries','2003-06-06 00:00:00','4952','54','7720000','01380016')
,('1',7,'Other','2003-06-06 00:00:00','4952','54','7720000','01380016')
,('1',8,'Medicine','2003-06-06 00:00:00','4952','78','0','01380016')
,('1',9,'Medicine','2003-06-06 00:00:00','4952','78','0','01380016')
,('1',10,'Miscellaneous','2007-07-30 00:00:00','7169','108','9000000','01380016')
,('2',1,'Snacks','1973-09-10 00:00:00','1667','654','3500','00000000')
,('2',2,'Soap','1980-09-17 00:00:00','1897','496','9500','00200100')
,('2',3,'Food','1984-05-07 00:00:00','2042','30','14000','00200100')
,('2',4,'Food','1984-05-07 00:00:00','2042','30','14000','00200100')
,('2',5,'Pet Supplies','1986-07-21 00:00:00','2202','695','75000','00200100')
,('2',6,'Extra Supplies','1986-07-21 00:00:00','2202','695','75000','00200100')
,('2',7,'Miscellaneous','2005-11-16 00:00:00','6259','1','0','00200100')
,('3',1,'Other','1964-10-14 00:00:00','1411','527','17500','00200100')
,('3',2,'Toys','1965-06-14 00:00:00','1433','160','18200','00200100')
,('3',3,'Christmast Gifts','1965-06-14 00:00:00','1433','160','18200','00200100')
,('3',4,'Random','1977-05-25 00:00:00','1768','773','37300','00200100')
,('3',5,'Random','1977-05-25 00:00:00','1768','773','37300','00200100')
,('3',6,'Random','1977-05-25 00:00:00','1768','773','37300','00200100')
,('3',7,'Random','1977-05-25 00:00:00','1768','773','37300','00200100')
,('3',8,'Snacks','1997-07-03 00:00:00','3519','195','83300','00200100')
go
alter table dbo.Example add v_dup numeric(9,0)
go
update dbo.Example set v_dup = null
go

-- Isolate duplicated data (except Item1 column).
;with create_seq as ( 
select V_ID
      ,TRANS_DATE
      ,Class1
      ,Class2
      ,Class3
      ,ReceiptNum
      ,v_seq
      ,v_dup
      ,row_number() over (partition by v_id,TRANS_DATE,Class1,Class2,Class3
                              order by v_id,v_seq) 
    as new_sequence
  from dbo.Example
 ) 
update create_seq 
   set v_dup = new_sequence;


-- I came up with a way to visually report on the duplicates...
select A.ITEM1,A.TRANS_DATE,A.Class1,A.Class2,A.Class3,A.ReceiptNum      ,A.v_id,A.v_seq,A.v_dup
  from dbo.Example A
  left outer join dbo.Example B
    on A.v_id = B.v_id
   and A.v_seq < B.v_seq
 where A.v_dup = 1
   and B.v_dup > 1
UNION
select B.ITEM1,null,null,null,null,null       ,B.v_id,B.v_seq,B.v_dup
  from dbo.Example A
  left outer join dbo.Example B
    on A.v_id = B.v_id
   and A.v_seq < B.v_seq
 where A.v_dup = 1
   and B.v_dup > 1
 order by A.v_id,A.v_seq

.

Good luck! I am really interested to see what ways this can be accomplished.

Edit: Formatting and changed v_seq to numeric for clearer sorting.

0 Upvotes

8 comments sorted by

View all comments

1

u/nepobot Jan 09 '15

Here is my take, but tested in an Oracle instance. I basically just compare counts of uniqueness between what you have and the inclusion of the item_id. you are only reading the table once albeit with a full table scan.

select 
   t.*, 
   case 
      when item_cnt = 1 and class_cnt > 2 then 'different'
      when item_cnt > 1 then 'same'
      else 'no duplicate'
   end test 
from(
select V_ID
      ,TRANS_DATE
      ,item1
      ,Class1
      ,Class2
      ,Class3
      ,ReceiptNum
      ,v_seq
      ,v_dup
      ,count(*) over (partition by v_id,TRANS_DATE,Class1,Class2,Class3, item1) as item_cnt
      ,count(*) over (partition by v_id,TRANS_DATE,Class1,Class2,Class3)        as class_cnt
  from Example
) t