0

I wanted the following query to be executed,

  String selectQuery = "SELECT  max(score),name FROM " + TABLE_SCORE
                        +" group by name";

I get my output as random values. Actually I wanted the data from the maximum values to the least. Please let me know, how to write the query.

2 Answers 2

2

Actually I wanted the data from the maximum values to the least

You mean order by max to min value? Try this:

  SELECT SUM(score), name 
    FROM MyTable
GROUP BY name
ORDER BY SUM(score) DESC;

See this SQLFiddle

So your query should be like this:

String selectQuery = "SELECT SUM(score), name FROM " + TABLE_SCORE
                        +" GROUP BY name ORDER BY SUM(score) DESC";
Sign up to request clarification or add additional context in comments.

3 Comments

@hd1 - OP can't upvote right now :). And always use @username to reply particular user. :)
hey no its not accepting immediately. It told wait for 5 minutes. That's why i waited and voted up.
@PavithraMohan - Don't worry. That's the rule. No need to accept immediately. :)
0
String selectQuery = "SELECT  score, name FROM " + TABLE_SCORE
                        +" order by score, group by name";

This will give you the records according to the scores(Ascending order of score).

1 Comment

No, i am getting error when i run this query,05-06 05:03:02.983: E/AndroidRuntime(7354): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newandroid/com.example.newandroid.webview}: android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: SELECT max(score),name FROM score order by score group by name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.