Find targets for duplicate posts

0

Please login or register to vote for this query.

(click on this box to dismiss)

Each duplicate question has up to three other questions as the non-duplicate posts. This query finds the posts that duplicate questions point to. It mostly works, but no guarantees.

Moderators

Q&A for community managers, administrators, and moderators

create table #DupeStrings (Dupe INT, Targets VARCHAR(255),
    Tgt1 VARCHAR(255), Tgt2 VARCHAR(255), Tgt3 VARCHAR(255))
insert into #DupeStrings
select top 200 PostId as [Post Link],
    -- Grab text from after the first '[' to the first ']'
    substring(
        Text,
        charindex('[', Text) + 1,
        charindex(']', Text) - charindex('[', Text) - 1
    ) + ',,,',
    0, 0, 0
from PostHistory h
where PostHistoryTypeId = 10
and Comment in (1, 101)
order by h.CreationDate desc

-- this part is awful
update #DupeStrings
set tgt1 = left(Targets, charindex(',', Targets) - 1),
    tgt2 = substring(Targets, charindex(',', Targets) + 1, 100),
    tgt3 = substring(Targets, charindex(',', Targets) + 1, 100)

update #DupeStrings
set tgt2 = left(tgt2, charindex(',', tgt2) - 1),
    tgt3 = substring(tgt3, charindex(',', tgt3) + 1, 100)

update #DupeStrings
set tgt3 = left(tgt3, charindex(',', tgt3) - 1)

select Dupe,
    nullif(cast(tgt1 as int),0) as Tgt1,
    nullif(cast(tgt2 as int),0) as Tgt2,
    nullif(cast(tgt3 as int),0) as Tgt3
into #Dupes
from #DupeStrings

select * from #Dupes

select
    Dupe as [Post Link],
    Tgt1 as [Post Link],
    Tgt2 as [Post Link],
    Tgt3 as [Post Link]
from #Dupes
left join Posts d on Dupe = d.Id
left join Posts t1 on Tgt1 = t1.Id
left join Posts t2 on Tgt2 = t2.Id
left join Posts t3 on Tgt3 = t3.Id
where d.Tags not like '%story-identification%'
      and (t1.Tags like '%story-identification%'
           or t2.Tags like '%story-identification%'
           or t3.Tags like '%story-identification%')


--select Dupe as [Post Link], FirstTarget as [Post Link], TgtPost1.Tags
--from #Dupes
--left join Posts DupePost on DupePost.Id = Dupe
--left join Posts TgtPost1 on TgtPost1.Id = FirstTarget
--where DupePost.Tags not like '%<story-identification>%'
      --and TgtPost1.Tags like '%<story-identification>%'

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results