How can I create a column with a running sum of the wordcount using mysql? -


i'm using following sql combine 2 tables , count results

select td.word, count( instance ) wordcount         ws_words td         left join ws_missed_words t on ( t.wordid = td.wordid )         quizid =1         group td.word 

the results give

***************************** |  word  |      wordcount   | ***************************** |  list  |         3        | |  miss  |         1        | |  skip  |         1        | | another|         0        | 

i'd add 1 more column this. i'd running sum of wordcount column , put own column (called total) shown below.

wanted results

 ****************************************************     |  word  |      wordcount   |   totalwordcount  |     *************************************************     |  list  |         3        |     3             |     |  miss  |         1        |     4             |     |  skip  |         1        |     5             |     | another|         0        |     5             | 

i've tried following

select td.word, count( instance ) wordcount, sum(wordcount) totalwordcount ws_words td left join ws_missed_words t on ( t.wordid = td.wordid ) quizid =1 group td.word 

but error

#1054 - unknown column 'wordcount' in 'field list' 

so next tried

select atable.word, atable.wordcount, sum(atable.wordcount) totalwordcount (select td.word, count( instance ) wordcount ws_words td left join ws_missed_words t on ( t.wordid = td.wordid ) quizid =1 group td.word) atable 

but this

************************************************ |  word  |      wordcount   |   totalwordcount | ************************************************ |  list  |         1        |         5        | 

question

how can create column running sum of wordcount?

update strawberry's method gives me below code


|  word  |      wordcount   |   running         | ************************************************* | another|         0        |     1             | |  miss  |         1        |     2             | |  skip  |         1        |     3             | |  list  |         3        |     4             | 

it seems ranking each word, lowest wordcount starts @ 1, if there 2 words of equal wordcount lists them in alphabetical order.

untested, 'cause gave nothing test - 1 way this...

 select x.*, count(*) running          ( select td.word                   , count( t.instance ) wordcount                ws_words td                left                 join ws_missed_words t                   on t.wordid = td.wordid                td.quizid = 1                group                   td.word       ) x    join       ( select td.word              , count( t.instance ) wordcount           ws_words td           left            join ws_missed_words t              on t.wordid = td.wordid           td.quizid = 1           group              td.word       ) y      on y.wordcount < x.wordcount      or y.wordcount = x.wordcount     , y.word < x.word   group      x.word; 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -