几行之间的SQLite SUM() [英] SQLite SUM() between several rows

查看:830
本文介绍了几行之间的SQLite SUM()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,在Android应用程序的SUM功能。我有一个表,看起来像下面这样:

I need some help with the SUM feature in android app. I have a table that looks something like the following :

在这里输入的形象描述

我需要 SUM 最后两个记录笔记和与注意最后一条记录的数量。我需要总结的行数量31,32和33这将返回90.我试过

I have need to SUM Quantities between last two records Notes and last one record with Note. I need to sum Quantity of rows 31,32 and 33. It would return 90. I've tried

SELECT Sum(QUANTITY) FROM fuel_table WHERE NOTE!='' ORDER BY ID DESC

但它返回的所有量总和与注意事项。

but it returns SUM of all quantities with note.

推荐答案

我倾向于短语的问题是:从金额有一个音符超前他们中的所有行的数量。这表明:

I am inclined to phrase the question as: sum the quantity from all rows that have one note "ahead" of them. This suggests:

select sum(quantity)
from (select ft.*,
             (select count(*)
              from fuel_table ft2
              where ft2.note = 'Yes' and ft2.id >= ft.id
             ) as numNotesAhead
      from fuel_table ft
     ) ft
where numNotesAhead = 1;

这篇关于几行之间的SQLite SUM()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆