我一直试图想象如何使用CTE来实现这一点,因为它在表面上看起来是最好的方式,但却无法实现。也许它还需要临时表。我正在使用SQL Server 2008 R2。
我需要使用以下参数创建截距(基本上是一条线的长度)。
注意:因为我已经处理了这个,所以没有间隙,而且from和to可以是十进制
此处显示了一个示例:
在这里看到的空间中左侧的测定和右侧的深度:
因此,如果需要更加清晰,6至7和17至18的间隔不会成为较大截距的一部分,因为内部废物(7-9和/或15-17)会使平均值低于0.7 - 不是因为内部浪费的数量。
然而,21-22的结果不包括在内,因为它之间有3米的内部废物,结果为17-18。
请注意,有多个站点和区域构成了原始表主键的一部分,所以我想在任何ROW_NUMBER OVER语句中都会使用区域和站点的分区
编辑:原始代码在from和to(多个14到15)中有错误,这会让人感到困惑。没有任何重叠的东西可以简化事情。
要使用的示例值:
create table #temp_inter (
area nvarchar(10),
site_ID nvarchar(10),
d_from decimal (18,3),
d_to decimal (18,3),
assay decimal (18,3))
insert into #temp_inter
values ('area_1','abc','0','5','0'),
('area_1','abc','5','6','0.165'),
('area_1','abc','6','7','0.761'),
('area_1','abc','7','8','0.321'),
('area_1','abc','8','9','0.292'),
('area_1','abc','9','10','1.135'),
('area_1','abc','10','11','0.225'),
('area_1','abc','11','12','0.983'),
('area_1','abc','12','13','0.118'),
('area_1','abc','13','14','0.438'),
('area_1','abc','14','15','0.71'),
('area_1','abc','15','16','0.65'),
('area_1','abc','16','17','2'),
('area_1','abc','17','18','0.367'),
('area_1','abc','18','19','0.047'),
('area_1','abc','19','20','0.71'),
('area_1','abc','20','21','0'),
('area_1','abc','21','22','0'),
('area_1','abc','22','23','0'),
('area_1','abc','23','24','2'),
('area_1','abc','24','25','0'),
('area_1','abc','25','26','0'),
('area_1','abc','26','30','0'),
('area_2','zzz','0','5','0'),
('area_2','zzz','5','6','1.165'),
('area_2','zzz','6','7','0.396'),
('area_2','zzz','7','8','0.46'),
('area_2','zzz','8','9','0.111'),
('area_2','zzz','9','10','0.053'),
('area_2','zzz','10','11','0.057'),
('area_2','zzz','11','12','0.055'),
('area_2','zzz','12','13','0.03'),
('area_2','zzz','13','14','0.026'),
('area_2','zzz','14','15','0.194'),
('area_2','zzz','15','16','0.367'),
('area_2','zzz','16','17','0.431'),
('area_2','zzz','17','18','0.341'),
('area_2','zzz','18','19','0.071'),
('area_2','zzz','19','20','0.26'),
('area_2','zzz','20','21','0.659'),
('area_2','zzz','21','22','0.602'),
('area_2','zzz','22','23','2.436'),
('area_2','zzz','23','24','0.874'),
('area_2','zzz','24','25','3.173'),
('area_2','zzz','25','26','0.179'),
('area_2','zzz','26','27','0.065'),
('area_2','zzz','27','28','0.024'),
('area_2','zzz','28','29','0')