Raja Amer Khan
Raja Amer Khan

Reputation: 1519

How to make multiple group counts in MySQL

I want to display counts for 2 fields using group query.

Here is the table and the scenario: TABLE

email            campaign
[email protected]    25
[email protected]    25
[email protected]    26

The query should fetch this for me. It should tell that [email protected] is repeated 3 times and there are 2 unique campaigns for that email.

Result: [email protected] email_count=3 campaign_count=2

I tried many group queries but none of them helped.

Any solution for this problem?

Upvotes: 0

Views: 58

Answers (2)

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

Try this

SELECT email,SUM(DISTINCT  campaign),COUNT(email)  FROM `table` GROUP BY email 

Upvotes: 0

John Woo
John Woo

Reputation: 263693

This should be pretty straight forward, COUNT() allows DISTINCT keyword to count only unique values.

SELECT email, 
       COUNT(*) email_count, 
       COUNT(DISTINCT campaign) Unique_count
FROM   tableName
GROUP  BY email

Upvotes: 2

Related Questions