使用GROUP和SUBSELECT进行计算未给出正确的值。

huangapple go评论61阅读模式
英文:

Calculation with GROUP and SUBSELECT not giving right value

问题

I have an issue to get a properly calculated value from my query... I have 3 tables:

Table match:

id idEvent status
1 6 completed
2 6 completed
3 6 completed

Table match2player:

idMatch idPlayer party
1 32 "0"
1 53 "0"
1 55 "1"
1 56 "1"
2 8 "0"
2 52 "0"
2 54 "1"
2 57 "1"
3 8 "0"
3 32 "0"
3 54 "1"
3 55 "1"

Table set:

idMatch index partySetWon
1 1 "0"
1 2 "1"
1 3 "0"
2 1 "1"
2 2 "1"
3 1 "1"
3 2 "0"
3 3 "1"

The following query is throwing the output underneath:

SELECT
match2player.idPlayer AS 'Player',
COUNT(DISTINCT match.id) AS 'Matches played',

SUM(IF(`match2player`.`party`=
(SELECT set1.`partySetWon` FROM `tennisdb`.`set` set1 WHERE set1.`idMatch` = `set`.`idMatch` GROUP BY set1.`partySetWon` ORDER BY COUNT(set1.`partySetWon`) DESC LIMIT 1)
,1,0)) AS 'Matches Won',

SUM(IF(`match2player`.`party`=`set`.`partySetWon`, 1, 0)) AS 'Sets Won',
SUM(IF(`match2player`.`party`<>`set`.`partySetWon`, 1, 0)) AS 'Sets lost'

FROM tennisdb.set
LEFT JOIN tennisdb.match ON match.id = set.idMatch
LEFT JOIN tennisdb.match2player ON match2player.idMatch = set.idMatch
WHERE match.idEvent = 6 AND match.status IN ('completed','awarded')
GROUP BY match2player.idPlayer
ORDER BY Player;

Player,"Matches played","Matches Won","Sets Won","Sets lost"
8,2,0,1,4
32,2,3,3,3
52,1,0,0,2
53,1,3,2,1
54,2,5,4,1
55,2,3,3,3
56,1,0,1,2
57,1,2,2,0

The column "Matches won" is giving wrong value (too high) and I have no idea how to solve... I played with further groupings, windows, etc., none was delivering expected result...

The subselect shall identify which party won the majority of sets, and resulting the match. This works okay when I on top group over the matches (and not only the player). But I am not looking for a breakdown of individual matches, I want to get the stats over all matches...

英文:

I have an issue to get a properly calculated value from my query... I have 3 tables:

Table match:

id idEvent status
1 6 completed
2 6 completed
3 6 completed

Table match2player:

idMatch idPlayer party
1 32 "0"
1 53 "0"
1 55 "1"
1 56 "1"
2 8 "0"
2 52 "0"
2 54 "1"
2 57 "1"
3 8 "0"
3 32 "0"
3 54 "1"
3 55 "1"

Table set:

idMatch index partySetWon
1 1 "0"
1 2 "1"
1 3 "0"
2 1 "1"
2 2 "1"
3 1 "1"
3 2 "0"
3 3 "1"

The following query is throwing the output underneath:

SELECT 
	`match2player`.`idPlayer` AS &#39;Player&#39;,
    COUNT(DISTINCT `match`.`id`) AS &#39;Matches played&#39;,

	SUM(IF(`match2player`.`party`=
	(SELECT set1.`partySetWon` FROM `tennisdb`.`set` set1 WHERE set1.`idMatch` = `set`.`idMatch` GROUP BY set1.`partySetWon` ORDER BY COUNT(set1.`partySetWon`) DESC LIMIT 1)
	,1,0)) AS &#39;Matches Won&#39;,
    
    SUM(IF(`match2player`.`party`=`set`.`partySetWon`, 1, 0)) AS &#39;Sets Won&#39;,
    SUM(IF(`match2player`.`party`&lt;&gt;`set`.`partySetWon`, 1, 0)) AS &#39;Sets lost&#39;
FROM `tennisdb`.`set`
LEFT JOIN `tennisdb`.`match` ON `match`.`id` = `set`.`idMatch`
LEFT JOIN `tennisdb`.`match2player` ON `match2player`.`idMatch` = `set`.`idMatch`
WHERE `match`.`idEvent` = 6 AND `match`.`status` IN (&#39;completed&#39;,&#39;awarded&#39;)
GROUP BY `match2player`.`idPlayer`
ORDER BY Player;

Player,&quot;Matches played&quot;,&quot;Matches Won&quot;,&quot;Sets Won&quot;,&quot;Sets lost&quot;
8,2,0,1,4
32,2,3,3,3
52,1,0,0,2
53,1,3,2,1
54,2,5,4,1
55,2,3,3,3
56,1,0,1,2
57,1,2,2,0

The column "Matches won" is giving wrong value (too high) and I have no idea how to solve... I played with further groupings, windows, etc., none was delivering expected result...

The subselect shall identify which party won the majority of sets, and resulting the match. This works okay when I on top group over the matches (and not only the player). But I am not looking for a breakdown of individual matches, I want to get the stats over all matches...

答案1

得分: 0

分析内部查询中的比赛,然后汇总集合、赢得的比赛和输掉的比赛:

select idplayer, sum(case when setwon > setlost then 1 else 0 end) mwon,
       sum(case when setwon < setlost then 1 else 0 end) mlost,
       sum(setwon) swon, sum(setlost) slost
from (
  select mp.idmatch, idplayer, 
         sum(case party when partysetwon then 1 else 0 end) setwon,  
         sum(case party when partysetwon then 0 else 1 end) setlost  
  from match2player mp 
  join `set` s on s.idmatch = mp.idmatch 
  group by mp.idmatch, idplayer) grp
group by idplayer order by idplayer

dbfiddle 演示

英文:

Analyze matches in inner query and then sum sets, matches won and lost:

select idplayer, sum(case when setwon &gt; setlost then 1 else 0 end) mwon,
       sum(case when setwon &lt; setlost then 1 else 0 end) mlost,
       sum(setwon) swon, sum(setlost) slost
from (
  select mp.idmatch, idplayer, 
         sum(case party when partysetwon then 1 else 0 end) setwon,  
         sum(case party when partysetwon then 0 else 1 end) setlost  
  from match2player mp 
  join `set` s on s.idmatch = mp.idmatch 
  group by mp.idmatch, idplayer) grp
group by idplayer order by idplayer

dbfiddle demo

huangapple
  • 本文由 发表于 2023年5月24日 21:59:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定