Sqlite 使用合并减去列值
我想为race、bib、splitgroupby中返回的每条记录减去place列中的值,这样diff列就会像这样显示 期望输出: 我不熟悉使用coalesce语句,最接近预期输出的是以下内容Sqlite 使用合并减去列值,sqlite,Sqlite,我想为race、bib、splitgroupby中返回的每条记录减去place列中的值,这样diff列就会像这样显示 期望输出: 我不熟悉使用coalesce语句,最接近预期输出的是以下内容 select a.race,a.bib,a.split, a.place, coalesce(a.place - (select b.place from ranking b where b.split < a.split), a.place) as diff from ranking a
select a.race,a.bib,a.split, a.place,
coalesce(a.place -
(select b.place from ranking b where b.split < a.split), a.place) as diff
from ranking a
group by race,bib, split
谢谢你的关注 要计算差异,您必须在具有相同种族和bib值的行中查找值,以及下一个较小的分割值:
select a.race,a.bib,a.split, a.place,
coalesce(a.place -
(select b.place from ranking b where b.split < a.split), a.place) as diff
from ranking a
group by race,bib, split
race | bib | split | place | diff
----------------------------------
10 | 514 | 1 | 5 | 5
10 | 514 | 2 | 3 | 2
10 | 514 | 3 | 2 | 1
10 | 17 | 1 | 8 | 8
10 | 17 | 2 | 12 | 11
10 | 17 | 3 | 15 | 14
SELECT race, bib, split, place,
coalesce((SELECT r2.place
FROM ranking AS r2
WHERE r2.race = ranking.race
AND r2.bib = ranling.bib
AND r2.split < ranking.split
ORDER BY r2.split DESC
LIMIT 1
) - place,
0) AS diff
FROM ranking;