2017-01-31 2 views
0

다음 표를 다시 가져 오는 쿼리가 있습니다. 마지막 열은 Total_pointsGames_played으로 나누어 마지막 열에 Points_Per_Game 숫자를 얻습니다.이 쿼리로 인해 나눗셈이 잘못되는 이유는 무엇입니까?

이 테이블 여기

+----------+--------------+--------+-----------------+ 
| PlayerID | Games_Played | Points | Points_Per_Game | 
+----------+--------------+--------+-----------------+ 
|  16 |   2 |  3 |   0.6000 | 
|  20 |   2 |  3 |   0.6000 | 
|  10 |   2 |  3 |   0.3750 | 
|  18 |   2 |  3 |   0.3750 | 
|  5 |   2 |  3 |   0.3750 | 
|  9 |   2 |  3 |   0.3750 | 
+----------+--------------+--------+-----------------+ 
6 rows in set (0.00 sec) 

SELECT Players.PlayerID, COUNT(Games.Points) AS Games_Played, 
SUM(Games.Points) AS Points, SUM(Points/Games_Played) AS Points_Per_Game 
FROM Teams, Players, Games 

WHERE 
Teams.PlayerID=Players.PlayerID 
AND 
Games.Game=Teams.Game 
AND 
Games.Team=Teams.Team 
AND 
Games.GameDate=Teams.GameDate 
AND 
(
CONCAT(Games.Game, '_', Games.GameDate)= 
ANY(SELECT CONCAT(Teams.Game, '_', Teams.GameDate) 
FROM Teams WHERE PlayerID='4') 
AND NOT 
CONCAT(Games.Game, '_', Games.Team, '_', Games.GameDate)= 
ANY(SELECT CONCAT(Teams.Game, '_', Teams.Team, '_', Teams.GameDate) 
FROM Teams WHERE PlayerID='4') 

) 

GROUP BY Teams.PlayerID 
ORDER By Points_per_Game DESC, Points DESC, Games_Played; 

내가 의한 그룹화하고 참조하는 열을 합산하고 있다는 사실과 함께 할 수있는 뭔가가있는 쿼리입니다?

나는 당신의 Games_Played가 올바른지 또는 가장 오해의 소지가 아니었다 ....

SELECT Players.PlayerID, COUNT(Games.Game) AS Games_Played, 
SUM(Games.Points) AS Points, SUM(Games.Points)/COUNT(Games.Game) AS Points_Per_Game 
FROM Teams, Players, Games 

이 시도 1.5

+0

http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple- sql-query – Strawberry

+0

게임이 제대로 실행되지 않는다 – AntDC

+0

@Strawberry 나는 분명히 제안을 받아 들일 것이다. – denski

답변

1

로 나오는 않는 이유를 볼 수없는 것.

또한 팀, 게임 & 플레이어를 연결하는 데 JOINS를 사용하는 방법을 살펴볼 것을 권합니다.

+0

이전 주셔서 감사 합니다이 @AntDC Games_Played는 두 게임에서 맞습니다. 나는'SUM (SUM (Games.Points)/COUNT (Games.Game))'을 시도했지만 당신이 제안한 방식대로 시도하지는 않았지만 완벽하게 작동합니다. – denski

관련 문제