如何代表 SQLite 中的列不同值检索行值?

How to retrieve row values on behave of column distinct value in SQLite?

我有一个table这样的

  User-ID     RANK        Dept     LEVEL-PASSED         
 -------     ------       -----     --------  
    1          950         TECH        54
    2          525         TECH        32
    3          379         TECH        23
    4          955         AGRI        54
    5          956         AGRI        54
    6          558         AGRI        32

代表 "level passed" 列中的不同值,我想用它检索部门值。

我如何在 sql-lite 中做到这一点 android。

Cursor cursor = db.query(true, TABLE_NAME, new String[] { LEVEL-PASSED}, null, null, null, null, null, null);

这会检索不同的级别传递值,即 54、32、23,但我也想通过它检索部门值。

预期的结果是这样的。

Dept     LEVEL-PASSED 
TECH        54
TECH        32
TECH        23

我不在乎 Dept 有 TECH 还是 AGRI。 distinct 应该只是带来一些部门价值。

SQLite 在 group by ... 时允许在 select 列表中列出非聚合列,因此请使用此 功能 ,因为您不需要关心哪个部门将返回:

Cursor cursor = db.rawQuery("select dept, [level-passed] from " + TABLE_NAME + " group by [level-passed]", null);

如果您想要标准 SQL:

,则使用 MAX()MIN() 进行聚合
Cursor cursor = db.rawQuery("select max(dept) dept, [level-passed] from " + TABLE_NAME + " group by [level-passed]", null);