I would like to select duplicate records from a SQL Server table, but only if the identifier is consistent.
I am trying to flip this answer to my needs, but I cannot get it to work.
The above answer is for Oracle, but I see that SQL Server also has functions leadand lag.
Also, I think the answer above puts *next to duplicates, but I only want to select duplicates.
select
id, companyName,
case
when companyName in (prev, next)
then '*'
end match,
prev,
next
from
(select
id,
companyName,
lag(companyName, 1) over (order by id) prev,
lead(companyName, 1) over (order by id) next
from
companies)
order by
id;
An example :
So from this dataset:
id companyName
-------------------
1 dogs ltd
2 cats ltd
3 pigs ltd
4 pigs ltd
5 cats ltd
6 cats ltd
7 dogs ltd
8 pigs ltd
I want to choose:
id companyName
-------------------
3 pigs ltd
4 pigs ltd
5 cats ltd
6 cats ltd
Update
, SO. - . , , , SqlZim, , . . , : " ?".