开发者

Query the row which with the maximum value on "cont" column

Hey, I'm trying to query the words which have the maximul values in the cont columns. Ex: if the word "test1" has the value 5 in cont, and "test2" has 2, "test1" will be shown in first position. got it?

so. Im trying to do it but it returns the following error:

09-18 22:24:04.072: ERROR/AndroidRuntime(435): Caused by: android.database.sqlite.SQLiteException: misuse of aggregate function MAX(): , while compiling: SELECT word FROM words WHERE MAX(cont) ORDER BY cont desc

Here is my method:

public List<String> selectMaxCont(){
   List<String> list = new ArrayList<String>();
   Cursor cursor = this.db.query(TABLE_NAME, new String[]{"word"}, "MAX(cont)", null, null, null, "cont desc");
   if(cursor.moveToFirst()){
       do{
   list.add(cursor.getString(0));
       }while(cursor.moveToNext());
开发者_如何转开发   }
   if(cursor != null && !cursor.isClosed()){
       cursor.close();
   }
   return list;

}


I think you are trying to descending sort by the column cont:

In that case, try this:

select * from words order by cont desc;

You shouldn't need max() to sort by a column.

FYI:

max() is an aggregate function and needs to be selected.

select max(column) from table

only returns one value.

select * from TABLE where COLUMN < (select max(COLUMN) from TABLE);

returns rows where the value in COLUMN is less than the max in that column.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜