sql - Sqlite: Get Modal/Mode from two related columns -


this extract table games:

id  wuser       buser 1   ddyer       bdot 2   ddyer       ronaldinho 3   bluedog     ronaldinho 4   ronaldinho  bluedog 5   bluedog     ronaldinho 6   ddyer       bobc 7   bobc        ddyer 

the table database website boardspace.net in people play board games. in database wuser , buser white , black players, , id game played. want know players played most. white players:

 select wuser, count(wuser)   game   group wuser   order count(wuser) desc; 

result is:

wuser    count(wuser) dumbot   75926 guest    22541 image13  1594 drraven  1460 eucalyx  1147 loizz    963 

then have same black players:

 select buser, count(buser)   game   group buser   order count(buser) desc;  buser    count(buser) dumbot   59489 guest    29324 eucalyx  1007 image13  756 drraven  746 loizz    715 alexis   660 

i can sum manually did below, how result single sql statement:

user     count(as both buser , wuser) dumbot   135412 guest    51865 image13  2350 drraven  2206 

i want know total number of games played each player, not interested whether play white or black.

as got large number of rows per user better count before union , add aggregate step:

select usr, sum(cnt)  (    select wuser usr, count(*) cnt    game     group wuser     union     select buser, count(*)     game     group buser  ) dt  group usr  order sum(cnt) desc; 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -