I hope I understand your requirements.
There may be other ways to do this, but personally I would probably use temporary tables for temporary work if I did.
--select data into a temp table that can be modified select * into
SQLFiddle: http://sqlfiddle.com/#!3/f4aab/7
Sorry if there are errors here, but I'm sure you get the idea, its not rocket science, just a different approach
EDIT : you notice that you are interested in strings without a row id. Just if this is the only line, then this is not a problem. I changed select first (in # cleaned) to take all the rows.
EDIT : NO Temp Tables now you know what it does, the same thing here without any temporary tables, but a WARNING . This is an update of the source tables to assign the missing personal number.
update c set c.personalNo = s.personalNo from table1 as c inner join table1 as s on c.name = s.name and c.stringID = s.stringID and c.personalNo is null and s.personalNo is not null select distinct s.name ,case when i.name is not null then 1 else 0 end as issue from table1 as s left outer join ( select name ,PersonalNo ,count(*) as numIDs from( select name ,PersonalNo ,stringID from table1 group by name ,PersonalNo ,stringID ) as i group by name ,PersonalNo having count(*) > 1 ) as i on s.name = i.name and s.personalNo = i.personalNo order by issue desc
SQLFiddle: http://sqlfiddle.com/#!3/f4aab/8
PARITIONING I don’t see how I will use partitioning here, because what you want to do is known only if there is more than one row, I use partitioning from a more complex table or if I am going to rank decision results when updating data based on more complex rules .. but in any case, insertion is prohibited in the section: D
Select name ,personalNo ,case when numstrings > 1 then 1 else 0 end as issue from (select name ,personalNo ,row_number() over (partition by name ,personalNo order by name ,personalNo ,stringID ) as numstrings from
NOTE: this uses the # cleared table, as mentioned above, since I believe that the essence of what makes it difficult is sometimes the missing personal information.
No temporary tables, no updates
Working with the above, it is obvious that you can do without any temporary tables or update anything, it is just a matter of readability / maintainability, and also whether it is really even faster. this can be made more stable for handling string identifiers with multiple personal names:
select distinct s.name ,case when i.name is not null then 1 else 0 end as issue from table1 as s left outer join ( select name ,PersonalNo ,count(*) as numIDs from( select a.name ,coalesce(a.PersonalNo,b.PersonalNo) as PersonalNo ,a.stringID from table1 as a left outer join table1 as b on a.name = b.name and a.stringid=b.stringid and a.personalNo != b.personalNo and b.personalNo Is Not Null group by a.name ,a.PersonalNo ,a.stringID ,b.PersonalNo ) as i group by name ,PersonalNo having count(*) > 1 ) as i on s.name = i.name and s.personalNo = i.personalNo order by issue desc
SQLFiddle: http://sqlfiddle.com/#!3/f4aab/9
EDIT: searching for inconsistent personal numbers too - this uses one temporary table, but you can change it, as was done in the last example. NOTE. There is a slight deviation from the original structure that you set because, as it would be more, I would complete this task, but there is more than enough code for you so that you can flip whatever you want.
--select data into a temp table that can be modified select * into
SQLFiddle: http://sqlfiddle.com/#!3/e9da2/18
UPDATE: it is also required to accept an empty string personalNo This is another new requirement .. accept an empty string the same as NULL in personalNo
--select data into a temp table that can be modified select * into
SQLFiddle: http://sqlfiddle.com/#!3/412127/8