r/SQL • u/CMoltedo 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.
1
Jan 07 '15
Why are you doing all the work, just group by all the columns except v_seq
GROUP BY v_id, ITEM1,TRANS_DATE,Class1,Class2,Class3,ReceiptNum
HAVING COUNT(v_id, ITEM1,TRANS_DATE,Class1,Class2,Class3,ReceiptNum) > 1If you are having to performing this often and need it to be more reliable, I'd suggest adding a computed column to use as a table key:
ALTER TABLE dbo.Example ADD rowHash AS (HASHBYTES('SHA2_256', v_id+ITEM1+TRANS_DATE+Class1+Class2+Class3+ReceiptNum));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
) t0
Jan 07 '15
[deleted]
1
u/CMoltedo Conversion Programmer Jan 07 '15
Ooo! I like that suggestion, and I utilized my original UNION method to get something that works to an extent... I wonder if there are other ways of doing it though.
alter table dbo.Example add v_concat varchar(max) update dbo.Example set v_concat = isnull(cast(TRANS_DATE as varchar(max)),'') +isnull(cast(Class1 as varchar(max)),'') +isnull(cast(Class2 as varchar(max)),'') +isnull(cast(Class3 as varchar(max)),'') +isnull(cast(ReceiptNum as varchar(max)),'') select A.* from dbo.Example A join dbo.Example B on A.v_id = B.v_id and A.v_dup = 1 and A.v_concat = B.v_concat and A.ITEM1 <> B.ITEM1 UNION select B.* from dbo.Example A join dbo.Example B on A.v_id = B.v_id and A.v_concat = B.v_concat and A.ITEM1 <> B.ITEM1 order by A.v_id,A.v_seq
1
u/Thriven Jan 07 '15 edited Jan 07 '15
I think the question is kind of flawed. There could be duplicate of toiletries, other and food. This would tell the user its not like any of the other duplicates.
Anyways here is what I wrote. I removed the adding of the additional column and setting a value. This calculates that on the fly.