android - sqlite 求和来自同一日期的值并返回 1 行

标签 android sql sqlite

我正在编写一个 Android 应用程序,用于存储锻炼日期和燃烧的卡路里。下面是我的表中数据的示例: 这是返回:

SELECT month, day, calories FROM workouts ORDER BY year ASC, month ASC, day ASC LIMIT 12;

(month | day | calories)
3|2|714
3|3|945
3|4|630
3|10|446
3|16|396
3|20|255
3|22|108
3|23|112     
3|23|169
3|23|2160

我为上面写的代码是:

  public Cursor getLastTwelveDays(){
  Cursor c;
  String[] s = {KEY_MONTH, KEY_DAY, KEY_CALORIES};
  String order = KEY_YEAR + " ASC, " + KEY_MONTH + " ASC, " + KEY_DAY + " ASC LIMIT 12" ;
  c = db.query(WORKOUT_TABLE, s, null, null, null, null, order);
  return c;
}

我想将具有相同日期和月份的行组合成一行,如下所示:

3|2|714
3|3|945
3|4|630
3|10|446
3|16|396
3|20|255
3|22|108
3|23|2441    (112 + 169 + 2160)    

另外,如果能配合android的查询功能使用就更好了。

提前致谢。

最佳答案

您正在寻找的是 group by 子句。使用 SUM() 方法获取每个日期的总卡路里。我认为 group by 子句应该放在 ORDER BY 子句之前。

SELECT month, day, SUM(calories) FROM workouts GROUP BY month, day

关于android - sqlite 求和来自同一日期的值并返回 1 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5767391/

相关文章:

sql - 当 where 子句仅包含索引的第一列时,为什么不使用索引?

c# - 从 DbProviderFactories.GetFactory() 获取 Sqlite

python - 我应该如何处理Python中访问sqlite数据库的多个线程?

android - 更改选项卡标题样式,如 Play 商店

android - Android Application.getInstance 是否一直存在?

java - 如何将单个 ArrayList<CustomObj> 元素传递给另一个 Activity

自 Google App 版本 6 起 Android 语音识别器速度变慢

sql - 查找没有 "duplicates"的记录

android - SQLite:创建多个表与为大数据集过滤一个数据库

java - 解析大的 100mb xml 并将其存储到 sqlite 数据库中