my SQL skills aren't strong enough to figure this out, hope someone can help. This is on SQLite for Android:
I have a table 'attempts':
attempt_id | word | score | attempt_time
----------------------------------------
1 | w1 | 1 | 2011-09-01
2 | w1 | 2 | 2011-09-02
3 | w2 | 1 | 2011-09-02
4 | w3 | 1 | 2011-09-03
5 | w4 | 0 | 2011-09-03
6 | w1 | 0 | 2011-09-04
Each word has a score associated with it; I want to find the number of words that have a score >=1 for each day. Scores are preserved from day to do, so a score for day t should be reflected in day t+1. When a word goes to score 0, it should not be counted. The result should look something like this:
attempt_time | count(word)
--------------------------
2011-09-01 | 1 // w1 (1)
2011-09-02 | 2 // w1, w2 (2)
2011-09-03 | 3 // w1, w2, w3 (3)
2011-09-04 | 2 // w2, w3 (2) (w1 now has score 0)
There are lots of attempt_times, so you can't loop through them individually. I suspect I need to use a subquery to sum the each date and the previous dates, but still need a way to discard duplicates (e.g. only count w1 once).
It should do something like this, but need all the dates rather than just one:
select count(distinct word) from attempts where score > 0
and attempt_time <= '2011-09-03';
> 3
My alternative is to build a separate table that just keeps track of the totals on each day, but then I have to keep the data in sync.
Thanks much.
Edit: I've tried to add some more details to make the question more clear. Hope it helps.