public Cursor getStatsPercent(String startdate, String enddate) {
String query = "SELECT *, ROUND((count(sex)*100)/(SELECT count(sex) FROM salesTable)) AS '"+PERCENTAGE+"' FROM salesTable where timeStamp BETWEEN '"+startdate+"' AND '"+enddate+"' GROUP BY sex ORDER BY percentage DESC" ;
Cursor c = dbSales.rawQuery(query, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
一切正常,但我总是回到100%以下的价值。 我认为这与ROUND有关,但无法解决这个问题。
答案 0 :(得分:0)
public Cursor getStatsPercent(String startdate, String enddate) {
String query = "SELECT *, ROUND((count(sex)*100.0)/(SELECT count(sex) FROM salesTable)) AS '"+PERCENTAGE+"' FROM salesTable where timeStamp BETWEEN '"+startdate+"' AND '"+enddate+"' GROUP BY sex ORDER BY percentage DESC" ;
Cursor c = dbSales.rawQuery(query, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
我修好了,只需将* 100更改为* 100.0 因此,如果有人想在这些代码应该工作的日期之间计算数据库中值的百分比。