1
I have the following tables:
times
- time_id INT
- time_nome VARCHAR
rodadas
- rod_id INT
- rod_rodada INT
- rod_pontos DECIMAL (10,2)
- rod_fk_time INT
I need to add the score and group by team.
1
I have the following tables:
times
- time_id INT
- time_nome VARCHAR
rodadas
- rod_id INT
- rod_rodada INT
- rod_pontos DECIMAL (10,2)
- rod_fk_time INT
I need to add the score and group by team.
4
Actually to add up the score you need the function SUM
, and not the function COUNT
. The result would be something like this:
SELECT tim.time_nome,
SUM(rod.rod_pontos) AS total
FROM times tim
INNER JOIN rodadas rod ON rod.rod_fk_time = tim.time_id
GROUP BY tim.time_nome
ORDER BY 2 DESC,
tim.time_nome
In the query above the function SUM
will add up all values, separating them by what is determined in the grouper clause GROUP BY
. Therefore, all wheel points separated by time_nome
shall be summed and presented. In the clause ORDER BY
put the number 2
, indicating that the second column of the query
shall be used in descending order (in the case of the total score).
-2
I managed to solve.
SELECT t.time_nome, SUM(r.rod_pontos) PONTOS
FROM rodadas r
INNER JOIN times t
WHERE t.time_id = r.rod_fk_time
GROUP BY r.rod_time
ORDER BY SUM(r.rod_pontos) DESC
Browser other questions tagged mysql sql query
You are not signed in. Login or sign up in order to post.
I believe that it will not work for some reasons. Before the table
rodadas
does not have a columnrod_time
as specified in itswhere
. According to which columnrod_pontos
is not inside an aggregator function and appears in the field relation. Third you are using column informationrod_pontos
doing nothing with her.– Sorack
Now you must be right hehe;
– Fred