Perhaps you add a [CompanyID] column to the table and include it as part of the Primary Key with your HierarchID. All Global rows will be in the table with a CompanyID of say "0". Then for each company modification add another row for that hierarchy. Like this: try the different @companyID values to see the results:declare @yourTable table (hierarchy varchar(10) ,companyid int ,Val varchar(100) ,primary key (hierarchy, companyid) )insert @yourTable select '1.', 0, 'Data from global table'union all select '1.1', 0, 'Data from global table'union all select '1.2', 1, 'Modified by Company 1'union all select '1.2', 2, 'Modified by Company 2'union all select '1.2', 0, 'Data from global table'union all select '1.2.1', 0, 'Data from global table'declare @companyid intset @companyid = 1--set @companyid = 2--set @companyid = 3select hierarchy ,valfrom ( select hierarchy ,Val ,seq = row_number() over (partition by hierarchy order by case when @companyid = companyid then -1 else companyid end) from @yourTable ) dwhere seq = 1
Be One with the OptimizerTG