我如何计算值(重复除外)?

时间:2016-01-20 06:58:49

标签: java android sqlite count

如何计算列中的值(重复项除外)。我尝试这个代码,但我没有。谢谢你的帮助。 (对不起,我的英文不好)

long net = DatabaseUtils.longForQuery(db, "select date, count(*) from  mytable GROUP BY date", null);
nettext.setText(String.valueOf(net));     

For example:
+----+------------+
| id | date       |
+----+------------+
|  1 | 2006-10-08 |
|  2 | 2006-10-08 |
|  3 | 2006-10-09 |
+----+------------+

There is two different date (2006-10-08 and 2006-10-09)
Result must be = 2

2 个答案:

答案 0 :(得分:3)

这应该有效:

long net = DatabaseUtils.longForQuery(db, "select count(DISTINCT date) from  mytable GROUP BY date", null);

答案 1 :(得分:1)

我找到了解决方案。谢谢m.aibin。

long net = DatabaseUtils.longForQuery(db, "select count(DISTINCT date) from  mytable", null);