如何在SQLite查询中计算正在运行的SUM? [英] How do I calculate a running SUM on a SQLite query?
本文介绍了如何在SQLite查询中计算正在运行的SUM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取一列,该列是另一列之前的所有值的总和?
How do I get a column that is the sum of all the values before of another column?
推荐答案
通过将表本身与表连接起来(执行所谓的笛卡尔或交叉联接)。参见下面的示例。
You can do it by joining the table with itself (performing a so-called Cartesian or cross join). See the following example.
SELECT a.name, a.gdppc, SUM(b.gdppc)
FROM gdppc AS a, gdppc AS b WHERE b.gdppc <= a.gdppc
GROUP BY b.id ORDER BY a.gdppc;
给出一个包含国家及其人均GDP的表格,它将为您提供GDP总量的总和
Given a table containing countries and their per capita GDP it will give you a running total of the GDP figure.
Democratic Republic of Congo|329.645|329.645
Zimbabwe|370.465|700.11
Liberia|385.417|1085.527
Burundi|399.657|1485.184
Eritrea|678.954|2164.138
Niger|711.877|2876.015
Central African Republic|743.945|3619.96
Sierra Leone|781.594|4401.554
Togo|833.803|5235.357
Malawi|867.063|6102.42
Mozambique|932.511|7034.931
...
请注意,这可能会占用大量资源,因为如果表中包含N个元素,它将创建一个包含N * N个元素的临时表。我不会在大桌子上执行它。
Note that this can be a very resource-intensive operation, because if a table has N elements it will create a temporary table with N*N elements. I would not perform it on a large table.
这篇关于如何在SQLite查询中计算正在运行的SUM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文