2015-04-15 60 views
0

昨天我開始更新我的記分牌有一些新的colomns工作,並得到了一些好的建議這裏,所以可能我會如此大膽地問了另一個問題?總和(情況計算總歐元每名球員

簡單的SQL這裏,但我需要的案例()爲歐元summorized所以它表明獲得總歐元或失去了所有回合

SELECT t1.playerid, sum(t1.points) as totalpoints, count(t1.playerid) as rounds,avg(points) as avgpoints 
 
    
 
      ,Case 
 
      when count(t1.playerid) in(7,6) and (select points from pokermax_scores as t4 where t1.playerid=t4.playerid and t1.tournamentid=t4.tournamentid) = 38 then 15 
 
      when count(t1.playerid) in(7,6,5) and (select points from pokermax_scores as t4 where t1.playerid=t4.playerid and t1.tournamentid=t4.tournamentid) = 25 then 5 
 
      when count(t1.playerid) in(5,4) and (select points from pokermax_scores as t4 where t1.playerid=t4.playerid and t1.tournamentid=t4.tournamentid) = 38 then 10 
 
      when count(t1.playerid) = 7  and (select points from pokermax_scores as t4 where t1.playerid=t4.playerid and t1.tournamentid=t4.tournamentid) = 16 then 0 
 
      when count(t1.playerid) = 4  and (select points from pokermax_scores as t4 where t1.playerid=t4.playerid and t1.tournamentid=t4.tournamentid) = 25 then 0 
 
      else -5  
 
      End as Euro 
 

 
FROM pokermax_scores as t1 
 
group by t1.playerid 
 
order by avgpoints desc

回報: IMG 「http://i62.tinypic.com/213iud0.png

你可以看到歐元不summorized只是隨機的人..

+0

清理問題,所以它更容易理解的問題。 – Jorn

回答

0

這裏是解決方案,首先使該收集所有信息的原始一個subsquery,然後summorize這筆錢()在主查詢。

例如:

select t3.playerid,sum(t3.Euro) 
 
from 
 
(
 
    
 
select t2.playerid,t2.points,t2.tournamentid, 
 
     Case 
 
      when count(t1.playerid) in(7,6) and t2.points = 38 then 15 
 
      when count(t1.playerid) in(7,6,5) and t2.points = 25 then 5 
 
      when count(t1.playerid) in(5,4) and t2.points = 38 then 10 
 
      when count(t1.playerid) = 7  and t2.points = 16 then 0 
 
      when count(t1.playerid) = 4  and t2.points = 25 then 0 
 
      else -5  
 
      End as Euro 
 
FROM pokermax_scores as t1, pokermax_scores as t2 
 
where t1.tournamentid = t2.tournamentid 
 
group by t2.tournamentid, t2.playerid) as t3 
 
group by t3.playerid