payal
payal

Reputation: 421

Query to group and create a group total

My table is like this

id     views     country    
1       44        A
2       43        A
3       899       B
4       242       A
5       3204      B
etc.

I want to count how many times a country is occurring.

For example in above case country A is occurring 3 times. So for my query I want to be able to correctly show the how many times each record is occurring, but in addition to the count I want to add the views also. In above case I want that country A is occurring 3 times and with views as the sum of 44 + 43 + 242 = 329, but my query displays only the first views output: 44.

Here is my query

 SELECT count( * ) value, country, concat( country , '_', views ) prod 
 FROM tableA
 GROUP BY prod

Upvotes: 2

Views: 99

Answers (2)

Vikdor
Vikdor

Reputation: 24124

SELECT
    count( * ) value, 
    country, 
    concat(country , '_', SUM(views)) prod 
FROM
    tableA 
GROUP BY country

Upvotes: 1

eggyal
eggyal

Reputation: 125865

SELECT country, COUNT(*), SUM(views) FROM tableA GROUP BY country

See it on sqlfiddle.

Upvotes: 2

Related Questions