Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/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中获得运行总和?_Sql_Sqlite - Fatal编程技术网

我如何在sqlite中获得运行总和?

我如何在sqlite中获得运行总和?,sql,sqlite,Sql,Sqlite,我试图在sqlite中得到一个运行的总和。现在我有: SELECT type, count, SUM(count) as startIndex FROM ( SELECT things.type, COUNT(things.name) as count FROM things INNER JOIN thingGroup

我试图在sqlite中得到一个运行的总和。现在我有:

SELECT 
    type, 
    count, 
    SUM(count) as startIndex 
FROM 
    (
        SELECT 
            things.type, 
            COUNT(things.name) as count 
        FROM 
            things 
            INNER JOIN thingGroups 
            ON thingID = things.id 
            INNER JOIN groups 
            ON groups.id=thingGroups.groupID 
        WHERE 
            groups.name='Space' 
        GROUP BY things.type 
        ORDER BY type
    ) 
GROUP BY type, count
这给了我:

Name A, 2, 2
Name B, 3, 3
Name C, 3, 3
我正在寻找:

Name A, 2, 0
Name B, 3, 2
Name C, 3, 5

可以使用相关子查询执行此操作:

select TYPE, COUNT,
       (select count(*)
        from things t2 INNER JOIN
             thingGroups 
             ON thingID = t2.id INNER JOIN
             groups 
             ON groups.id=thingGroups.groupID 
        WHERE groups.name='Space' and
              t2.type < things.type
       ) as cumsum
from (SELECT things.type, COUNT(things.name) as count 
      FROM things INNER JOIN
           thingGroups 
           ON thingID = things.id INNER JOIN
           groups 
           ON groups.id=thingGroups.groupID 
       WHERE groups.name='Space' 
       GROUP BY things.type
      ) t
order by type

顺便说一下,order By应该始终位于最外层的查询中,除非您使用某种限制或top来获取行的子集。

您能给我们展示一个数据示例吗?这几乎是正确的,cumsum列的值为2、5、8。我希望得到值0,2,5。如果cumsum是一个领先的值,我算出了