0

我是 Java 编程新手,所以我需要一些帮助……我正在做这个积累的事情……

像这样..

Score             Accumulated
10                10
40                50
60                110
and so on.......

我这里有我的代码...

public int accumulateScores(){

        db = this.getReadableDatabase();

        String query = "SELECT score, (SELECT sum(score) FROM "
                + TABLE_NAME+ ")" + " AS accumulated" + " FROM " + TABLE_NAME;
        Cursor cursor = db.rawQuery(query, null);

        if (cursor != null)
            cursor.moveToFirst();
            Score score = new Score(Integer.parseInt(cursor.getString(0)), cursor.getInt(1));




    }

我应该怎么做才能返回列分数的总和..提前致谢..任何帮助将不胜感激..

4

2 回答 2

1

这应该以一种相当简单的方式完成您需要的操作,只需将所有行的 rowid 小于当前行的行相加即可。如果您在表格上有一个自动递增键,您可能希望使用该键来获得更可预测的分数排序。

SELECT Score, 
   (SELECT SUM(Score) 
    FROM table_name a 
    WHERE rowid <= table_name.rowid
   ) Accumulated
FROM table_name
ORDER BY rowid

一个用于测试的 SQLfiddle

编辑:根据您的错误样本;

SELECT score, 
   (SELECT SUM(score) 
    FROM score_table a
    WHERE a.id <= score_table.id
   ) Accumulated
FROM score_table
ORDER BY id

另一个 SQLfiddle

于 2013-07-21T18:33:17.593 回答
0

你应该更多地使用谷歌并输入这个代码

SELECT SCORE, (SELECT SUM(SCORE) FROM table_name PATING WHERE rowid <=table_name.rowid )Accumulated FROM table_name ORDER BY rowid

于 2013-07-21T18:35:30.963 回答