First of all, sorry if it is a duplicated question...
I have an Android App (SQLITE) which check Lotery games. Basically, this lottery draws 5 numbers among 40 and I save them as follows:
|---------------------------------------------------------------------------|
| TABLE: lotery |
|---------------------------------------------------------------------------|
| game_number | NUM1 | NUM2 | NUM3 | NUM4 | NUM5 |
|---------------------------------------------------------------------------|
| 1 | 02 | 32 | 33 | 34 | 40 |
| 2 | 02 | 14 | 16 | 39 | 40 |
| 3 | 01 | 11 | 12 | 25 | 27 |
| 4 | 02 | 22 | 23 | 30 | 36 |
| ...... |
|---------------------------------------------------------------------------|
Now, I would like to consult how frequent a number is selected.
Example:
- Number 01 was drawn 1 time (in game_number==3)
- Number 02 was drawn 3 times (in game_number==1, game_number==2 and game_number==4))
- Etc...
What I have today:
SELECT * FROM
(select count(*) FROM lotery where 1 IN(NUM1,NUM2,NUM3,NUM4,NUM5)),
(select count(*) FROM lotery where 2 IN(NUM1,NUM2,NUM3,NUM4,NUM5)),
(select count(*) FROM lotery where 3 IN(NUM1,NUM2,NUM3,NUM4,NUM5)),
....
(select count(*) FROM lotery where 40 IN(NUM1,NUM2,NUM3,NUM4,NUM5));
This will produce:
|-----------------------------------------------------------|
| count(*) | count(*) | count(*) | ...... | count(*) |
|-----------------------------------------------------------|
| 1 | 3 | 0 | ...... | 2 |
|-----------------------------------------------------------|
First column correspond to "01". Second Column correspond to number "02". Etc... until 40th Column
Finally the Questions
Current implementation has low performance in some old and low cost devices.
So, I have following questions:
Do you see a better query for current table implementation?
Is it possible to query and get the results ordered?
Today, I perform that query and I manually sort the results in DESC order. My activity show the results in a listview in DESC order.
Since I'm using a list view, I don't need all results at once. For example, I could query only first 10 more frequent numbers (to fill the screen) and only consult remain numbers if the user scroll the list view.
Anyway, if you see any possible improvement in current implementation, I will appreciate...
Thanks a lot in advance!!