#### Calculating a fields value according to the values of the previous and next fields

``````carID           mileage           date
-----------------------------------------
1               400            01/01/2015
2               NULL           01/02/2015
3               NULL           01/03/2015
4               1050           01/04/2015
``````

``````, carKMcombiDiffList as (
select ml.*,
(ml.KM - mlprev.KM) as diff
from carKMcombilist ml outer apply
(select top 1 ml2.*
from carKMcombilist ml2
where ml2.FK_CarID = ml.FK_CarID and
ml2.beginmonth < ml.beginmonth
order by ml2.beginmonth desc
) mlprev
)
``````

2 个回复

``````with data as --test data
(
select * from (VALUES
(0, null, getdate()),
(1, 400, '20150101'),
(1, null, '20150201'),
(1, null, '20150301'),
(1, 1050, '20150401'),
(2, 300, '20150101'),
(2, null, '20150201'),
(2, null, '20150301'),
(2, 1235, '20150401'),
(2, null, '20150501'),
(2, 1450, '20150601'),
(3, 200, '20150101'),
(3, null, '20150201')
) as v(carId, mileage, [date])
where v.carId != 0
)
-- replace 'data' with your table name
select  d.*,
(select top 1 mileage from data dprev where dprev.mileage is not null and dprev.carId = d.carId and dprev.[date] <= d.date order by dprev.[date] desc) as 'Prev available mileage',
(select top 1 mileage from data dnext where dnext.mileage is not null and dnext.carId = d.carId and dnext.[date] >= d.date order by dnext.[date] asc) as 'Next available mileage'
from    data d
``````

`ri`缺少里程的连续组中的记录索引
`gi`每辆车缺少里程的连续组的索引
`gc`缺少里程的每个连续组的记录数

`pa` （以前可用）和
`na` （下一个可用）。

SQL小提琴： SO 29363187

``````with data as --test data
(
select * from (VALUES
(0, null, getdate()),
(1, 400, '20150101'),
(1, null, '20150201'),
(1, null, '20150301'),
(1, 1050, '20150401'),
(2, 300, '20150101'),
(2, null, '20150201'),
(2, null, '20150301'),
(2, 1235, '20150401'),
(2, null, '20150501'),
(2, 1450, '20150601'),
(3, 200, '20150101'),
(3, null, '20150201')
) as v(carId, mileage, [date])
where v.carId != 0
),
-- replace 'data' with your table name
limits AS
(
select  d.*,
(select top 1 mileage from data dprev where dprev.mileage is not null and dprev.carId = d.carId and dprev.[date] <= d.date order by dprev.[date] desc) as pa,
(select top 1 mileage from data dnext where dnext.mileage is not null and dnext.carId = d.carId and dnext.[date] >= d.date order by dnext.[date] asc) as na
from    data d
),
t1 as
(
SELECT l.*,
case when mileage is not null
then null
else row_number() over (partition by l.carId, l.pa, l.na  order by  l.carId, l.[date])
end as ri,   -- index of record in a continuous group where mileage is missing
case when mileage is not null
then null
else dense_rank() over (partition by carId order by  l.carId, l.pa, l.na)
end as gi    -- index of  a continuous group where mileage is missing per car
from limits l
),
t2 as
(
select  *,
(select count(*) from t1 tm where tm.carId = t.carId and tm.gi = t.gi)  gc  --count of records per continuous group where mileage is missing
FROM    t1 t
)
select  *,
case when mileage is NULL
then pa + (na - pa) / (gc + 1.0) * ri   -- also converts from integer to decimal
else NULL
end as 'Interpolated value'
from    t2
order by carId, [date]
``````

``````select with_dates.*,
prev_mileage.mileage as prev_mileage,
next_mileage.mileage as next_mileage,
next_mileage.mileage - prev_mileage.mileage as mileage_delta,
datediff(month,prev_d,next_d) as month_delta,
(next_mileage.mileage - prev_mileage.mileage)/datediff(month,prev_d,next_d)*datediff(month,prev_d,with_dates.d) + prev_mileage.mileage as estimated_mileage
from (select *,
(select top 1 d
from mileage as prev
where carid = c.carid
and prev.d < c.d
and prev.mileage is not null
order by d desc ) as prev_d,
(select top 1 d
from mileage as next_rec
where carid = c.carid
and next_rec.d > c.d
and next_rec.mileage is not null
order by d asc) as next_d
from mileage as c
where mileage is null) as with_dates
join mileage as prev_mileage
on     prev_mileage.carid = with_dates.carid
and prev_mileage.d = with_dates.prev_d
join mileage as next_mileage
on     next_mileage.carid = with_dates.carid
and next_mileage.d = with_dates.next_d
``````

1 使用MySQL中的上一个值来计算下一个字段？

2013-10-30 15:15:18 3 124   sql
2 根据下一个字段中的值选择或不选择一个字段

2012-08-27 09:33:14 3 56   php/ sql
3 HTML表单，在完成上一个字段后生成下一个字段

2017-02-28 17:21:12 0 33   html
4 如何在SQL Server中添加上一个字段和下一个字段？

5 如何使用“下一个上一个”按钮转到下一个字段

6 javascript更新下一个/上一个输入字段值

8 当字段值达到最大长度时，如何关注下一个字段

9 读取 Elementor 表单字段的值，并将其用于下一个字段

10 如果缺少字段值并且用户开始为下一个字段键入内容，如何显示错误消息？