Android Sqlite计算:如何精确地汇总两列并将一列总和除以另一列总和

时间:2018-03-29 05:30:11

标签: java android sqlite sum divide

伙计根据我的时区我应该说早上好

我正在开发一个能够组合或连接两个表并按id显示的android项目,我现在想知道如何将两列专门用于总结并将一列总和除以同一函数中的另一列总和

到目前为止我的进展

  public List<Courses> getListCourses(int id) {
    Courses courses = null;
    List<Courses> coursesList = new ArrayList<>();
    openDatabase();

    Cursor cursor = mDatabase.rawQuery("SELECT * FROM COURSES C INNER JOIN SEMESTER S ON S.semester_id=C.semester_id AND S.LEVEL_ID = C.LEVEL_ID AND S.LEVEL_CODE = C.LEVEL_CODE WHERE C.semester_id =  ?", new String[]{String.valueOf(id)});

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        courses = new Courses(cursor.getInt(0), cursor.getInt(1), cursor.getString(2), cursor.getInt(3), cursor.getInt(4), cursor.getString(5), cursor.getInt(6), cursor.getString(7), cursor.getInt(8));
        coursesList.add(courses);
        cursor.moveToNext();
    }
    cursor.close();
    closeDatabase();
    return coursesList;
}

这是两个表

学期表

   Id  |   semester_id    |   semester_code    |   level
   -------------------------------------------------
   1   |   1              |   1st Semester     |   100       
   2   |   2              |   2nd Semester     |   100        
   3   |   3              |   1st Semester     |   100       
   4   |   4              |   2nd Semester     |   100        
   5   |   5              |   1st Semester     |   100        

课程表

 Id  |Se_id| grade |  level |  semester     |  credit_unit  |  gp_unit
 ----------------------------------------------------------------------
 62  |  1  |  A   |  100   |  1st Semester  |  2           |  5
 63  |  1  |  B   |  100   |  1st Semester  |  2           |  4
 64  |  2  |  C   |  100   |  2nd Semester  |  2           |  3
 65  |  2  |  A   |  100   |  2nd Semester  |  2           |  5
 66  |  1  |  C   |  100   |  1st Semester  |  3           |  2

我想总结一下CREDIT_UNIT列并总结一下GP_UNIT列并将GP_UNIT除以CREDIT_UNIT总结一下我就不知道怎么样将它添加到此

 Cursor cursor = mDatabase.rawQuery("SELECT * FROM COURSES C INNER JOIN SEMESTER S ON S.semester_id=C.semester_id AND S.LEVEL_ID = C.LEVEL_ID AND S.LEVEL_CODE = C.LEVEL_CODE WHERE C.semester_id =  ?", new String[]{String.valueOf(id)});

1 个答案:

答案 0 :(得分:0)

您可以使用 sum方法,然后将其存储在变量

中进行除法
int creditunit,gpunit;
cursor = db.rawQuery("select sum(credit_unit) from FROM COURSES C INNER JOIN SEMESTER S ON S.semester_id=C.semester_id AND S.LEVEL_ID = C.LEVEL_ID AND S.LEVEL_CODE = C.LEVEL_CODE WHERE C.semester_id =  ? ;", new String[]{String.valueOf(id)});
if(cursor.moveToFirst())
    creditunit =cursor.getInt(0);
else
    creditunit = -1;
cursor.close();

cursor = db.rawQuery("select sum(gp_unit) FROM COURSES C INNER JOIN SEMESTER S ON S.semester_id=C.semester_id AND S.LEVEL_ID = C.LEVEL_ID AND S.LEVEL_CODE = C.LEVEL_CODE WHERE C.semester_id =  ? ;", new String[]{String.valueOf(id)});
if(cursor.moveToFirst())
    gpunit =cursor.getInt(0);
else
    gpunit = -1;
cursor.close();
//perform division
double sol= creditunit/gpunit;

编辑:查询是,

 cursor = db.rawQuery("select sum(credit_unit),sum(gp_unit) from FROM COURSES C INNER JOIN SEMESTER S ON S.semester_id=C.semester_id AND S.LEVEL_ID = C.LEVEL_ID AND S.LEVEL_CODE = C.LEVEL_CODE WHERE C.semester_id =  ? ;", new String[]{String.valueOf(id)});

<强>更新

要访问活动中的适配器类代码,

将您的任务作为活动中的方法,

public void getsolution(double sol)
{
    //code for displaying sol in textview
}

然后在你的适配器类中,

1.在构造函数中传递活动引用

public adapter(YourActivity activity)

2.执行sql代码后,调用activity方法

 activity.getsolution(sol);