Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite 使用合并减去列值_Sqlite - Fatal编程技术网

Sqlite 使用合并减去列值

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

我想为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
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;