答案 0 :(得分:1)
只需使用sfl.getCompleteValue().getSharedFolderId();
并将其与LEAD
(读取表数据一次)结合使用,就像这样
subquery
但是我按select tt.PersonId, tt.PreviousPosition, tt.NextPosition,
tt.NextMeter - tt.previousMeter
from (select PersonId, t.position PreviousPosition,
LEAD(Position) OVER(order by Position) NextPosition,
t.Meter PreviousMeter,
LEAD(Meter) OVER(order by Position) NextMeter
from myTable t) tt
where tt.NextPosition is not null
排序,如果你有一些键(继续插入排序)或者你有一些特殊的逻辑与惰性和排序只使用排序中的列
答案 1 :(得分:0)
尝试这样的事情。
SELECT id, position as previous,(
SELECT TOP 1 position
FROM table t1
WHERE ti.position > t2.position
ORDER BY
position
) as next,
(
SELECT TOP 1 meter
FROM table t1
WHERE ti.position > t2.position
ORDER BY
position
) - meter AS gap
FROM table t2
ORDER BY position
答案 2 :(得分:0)
试试这个(我必须为您的数据添加一个独特的PK列)
CREATE TABLE #Data
(
ID INT,
PersonId INT,
Position INT,
Meter INT
)
INSERT INTO #Data VALUES (1,1,10,10),(2,1,22,20),(3,1,34,35),(4,1,46,46)
SELECT PersonId,
Position AS PreviousPosition,
LEAD(Position) OVER (ORDER BY ID) NextPosition,
Meter as ThisMeter,
LEAD(Meter) OVER (ORDER BY ID) NextMeter
INTO #Results
FROM #Data
SELECT PersonId,
PreviousPosition,
NextPosition,
NextMeter - ThisMeter AS MetreGap
FROM #Results
WHERE NextPosition IS NOT NULL
DROP TABLE #Results
DROP TABLE #Data
答案 3 :(得分:0)
您可以尝试以下查询以获取所需的输出
select t1.persionid,t1.position prev_position,t2.position next_position,t2.meter-t1.meter meter_gap from
(select personid,position,meter,row_number() over( order by position,meter) rn1 from table) as t1 join (select personid,position,meter,row_number() over( order by position,meter) rn2 from table) as t2 on (t1.rn1+1=t2.rn2)