1

異なる値をスコアとして数えたい。カウントしたい値は7から10です。表にも1から6がありますが、カウントしたくありません。私はそれを正しく理解することはできません。

表の値は次のようになります。

RPO RSP RSV  
10 9 9
9 10 8
10 7 7
7 10 8
4 4 3

結果は次のようになります。

スコアRPORSPRSV
10 2 2 0
9 1 1 1
7 1 1 1

これが私のコードです。それを改善する必要があります。

select  

count(rank.rpo) as RPO,
count(rank.rsp) as RSP,
count(rank.rsv) as RSV

from
round 
left join base 
on round.id = base.round_id 

left join rank 
on round.id = rank.round_id and rank.number = base.number

where 
base.result = 1 
and round.round_date between '2013-03-15' and '2013-03-22'
and round.gameform = 'V4'
and round.gameform not like "OSPEC"

group by ??
4

1 に答える 1

2

あなたはこのようなことを試すことができます:

drop table if exists ids;

create table ids (score int unsigned primary key)
select distinct rpo as score from rank
union
select distinct rsp as score from rank
union
select distinct rsv as score from rank;

select ids.score,
       sum(if(rank.rpo=ids.score,1,0)) as RPO,
       sum(if(rank.rsp=ids.score,1,0)) as RSP,
       sum(if(rank.rsv=ids.score,1,0)) as RSV
  from ids,round 
left join base on round.id = base.round_id 
left join rank on round.id = rank.round_id and rank.number = base.number
 where base.result = 1 
   and round.round_date between '2013-03-15' and '2013-03-22'
   and round.gameform = 'V4'
   and round.gameform not like "OSPEC"
   group by ids.score with rollup;

一時テーブルを作成したくない場合は、次のことを試してください。

select ids.score,
       sum(if(rank.rpo=ids.score,1,0)) as RPO,
       sum(if(rank.rsp=ids.score,1,0)) as RSP,
       sum(if(rank.rsv=ids.score,1,0)) as RSV
  from 
  (
      select distinct rpo as score from rank
      union
      select distinct rsp as score from rank
      union
      select distinct rsv as score from rank
  ) ids, round 
left join base on round.id = base.round_id 
left join rank on round.id = rank.round_id and rank.number = base.number
 where base.result = 1 
   and round.round_date between '2013-03-15' and '2013-03-22'
   and round.gameform = 'V4'
   and round.gameform not like "OSPEC"
group by ids.score with rollup;

実用的な例については、 http://sqlfiddle.com/#!2/9b7d7/6を参照してください。

于 2013-03-24T21:08:57.517 に答える