drop view if exists RDS.V_INSURER_REGION;
CREATE VIEW RDS.V_INSURER_REGION
AS
select t.InsurerRegionID, t.Name, 0 LeftIndex, 0 RightIndex, 0 TreeLevel, null ParentID, rr.RegionID Region
from
(select t.TF_OKATO as InsurerRegionID, MAX(r.Name) Name
from RDS.T_F002_SMO t
join RDS.T_REGION r on SUBSTRING(r.OKATO, 1, 5) = t.TF_OKATO
group by t.TF_OKATO) t
left join RDS.t_region rr on t.Name= rr.Name
;
