Reputation: 1519
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
Reputation: 64466
Try this
SELECT email,SUM(DISTINCT campaign),COUNT(email) FROM `table` GROUP BY email
Upvotes: 0
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