Count rows that have the same value in 2 different columns?

Asked

Viewed 337 times

2

Table

ID    jogada    pontos
1       1          3
1       1          1
2       5          1 
2       5          1
2       5          3
2       5          3

I wanted to create a query that COUNTS the playing lines and ADDS the total of points, but that indicates the sum by repeating the "ID" like this:

Return I hope:

ID    contar_jogada    somar_pontos
1       2                   4
2       4                   8 

1 answer

4

Use the function SUM to add up the points, COUNT to count moves and Group By to group by ID.

Query

SELECT id AS ID, 
Count(jogada) AS TotalJogada, 
Sum(pontos)   AS Pontos 
FROM   tablename 
GROUP  BY id 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.