Alex Lockwood
Alex Lockwood

Reputation: 83313

Android - Request SQLite query to be sorted based on relationship between two columns

Is there any way to query a Cursor with the content sorted based on the row's relationship between two columns?

For example, if I had two columns in my table:

  1. Basketball shots made
  2. Basketball shots attempted

is there any way I query a Cursor with the data sorted based on the "shot percentage" (i.e. made/attempted)?

Upvotes: 0

Views: 448

Answers (2)

Alex Lockwood
Alex Lockwood

Reputation: 83313

Note that in order to get accurate sorting, you might need to cast to DOUBLE (the above suggestions does integer division which didn't provide correct results in all cases):

ORDER BY CAST(ShotsMade AS DOUBLE)/ShotsAttempted DESC

Upvotes: 2

StilesCrisis
StilesCrisis

Reputation: 16290

In your query, put ORDER BY ShotsMade/ShotsAttempted DESC.

Upvotes: 1

Related Questions