MANIPULER LACUNES À JOUR Juxtaposables
Ce n'est pas la meilleure approche, mais je suis sûr que cela fonctionne avec vos données de test. C'était amusant de jouer avec de toute façon. Je ne comprends pas l'utilisation réelle en elle, et juste parce que vous peut ne signifie pas que vous devriez tout le temps, mais encore une fois ce fut un défi amusant :)
;with cte1 as(
select
FullName,
City,
FromRight,
ToRight,
FromLeft,
ToLeft,
case
when lag(ToLeft) over(PARTITION BY FullName, City ORDER BY FullName, City, FromRight) is null or lag(ToLeft) over(PARTITION BY FullName, City ORDER BY FullName, City, FromRight) + 1 <> FromRight then FromRight
end as NewFromRight,
case
when lead(FromRight) over (PARTITION BY FullName, City ORDER BY FullName, City, FromRight) - 1 = ToLeft then NULL
when lead(FromRight) over (PARTITION BY FullName, City ORDER BY FullName, City, FromRight) is null then ToLeft
when lead(FromRight) over (PARTITION BY FullName, City ORDER BY FullName, City, FromRight) - 1 <> ToLeft then ToLeft
else ToLeft
end as NewToLeft
from #cities),
------this CTE is needed because I couldn't figure out how to do it without it
------It takes the max of the previous CTE for the given partition.
------Nested windows functions aren't allowed hence the second cte
cte2 as(
select distinct
FullName,
City,
NewFromRight as FromRight,
NewToLeft as ToLeft
from
cte1
where
NewFromRight is not null and NewToLeft is not null
union all
select distinct
FullName,
City,
--max(NewFromRight) over (PARTITION BY FullName, City ORDER BY FullName, City, FromRight) as FromRight,
--max(NewToLeft) over (PARTITION BY FullName, City ORDER BY FullName, City, FromRight) as ToLeft
case
when NewFromRight is null then lag(NewFromRight) over (PARTITION BY FullName, City ORDER BY FullName, City, FromRight)
else NewFromRight
end as FromRight,
case
when NewToLeft is null then lead(NewToLeft) over (PARTITION BY FullName, City ORDER BY FullName, City, FromRight)
else NewToLeft
end as ToLeft
from cte1
where
NewFromRight is null or NewToLeft is null)
select * from cte2
where FromRight is not null and ToLeft is not null
order by FullName, FromRight
Si quelqu'un veut jouer avec ça ... voici quelques données de test. Il suffit de remplacer YourTable
dans le premier CTE avec #cities
select
'W Main St' as FullName,'Townsville' as City,100 as FromRight,198 as ToRight,101 as FromLeft,199 as ToLeft
into #cities
UNION ALL SELECT 'W Main St','Townsville',200,298,201,299
UNION ALL SELECT 'W Main St','Townsville',500,598,501,599
UNION ALL SELECT 'W Main St','Cityburg',600,698,601,699
UNION ALL SELECT 'E 1st Ave','Townsville',100,398,301,399
UNION ALL SELECT 'E 1st Ave','Townsville',400,416,401,417
UNION ALL SELECT 'E 1st Ave','Townsville',418,458,419,459
UNION ALL SELECT 'E 1st Ave','Townsville',470,458,419,479
UNION ALL SELECT 'E 1st Ave','Townsville',490,458,419,499
UNION ALL SELECT 'E 1st Ave','Townsville',500,458,419,501
Êtes-vous sûr, ressemble 'E 1st Ave | Townsville | 100 | 398 | 301 | 399' manque 101..299. – Serg
votre ensemble de résultats suppose que le droit sera toujours inférieur à gauche, mais considérez que la plage de droite est entièrement dans la plage de gauche, par ex. NW 2nd st FromRight 150 ToRight 180 FromLeft 101 ToLeft 199 puis la rangée suivante est FromRight 200 ToRight 298 FromLeft 201 ToLeft 199. Dans ce cas, quel serait le total FromRight? Cela ne peut pas être 150 parce que cela couperait 101-149 pour le côté gauche de la rue. Serait-ce alors FromLeft - 1 comme FromRight? – Matt
Je suppose qu'avec les nuances je devrais me demander pourquoi combiner/simplifier les dossiers? Serait-il préférable de conserver les enregistrements supplémentaires et juste agréger après votre match? ou attribuer un numéro de bloc à un segment et l'agréger de cette façon? – Matt