Count function - Mysql

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 punctuation and group by time.

    
asked by anonymous 18.05.2017 / 19:09

2 answers

4

In fact to add the punctuation you need the function SUM , not the COUNT function. The result would be something like the following:

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 above query the SUM function will sum all the values, separating them by what is determined in the GROUP BY grouping clause. Thus, all round points separated by time_nome will be summed and displayed. In the ORDER BY clause I put the number 2 , indicating that the second column of query will be used in descending order (In case of total score).

    
18.05.2017 / 19:38
-2

I was able to resolve it.

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
    
18.05.2017 / 19:10