英文:
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 '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...
答案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
英文:
Analyze matches in inner query and then sum sets, matches won and lost:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论