Count the number of occurrences of each email address - mysql

I have a mySQL workbench table called table_contacts, with the fields:
user_id and PrimaryEmail
I want to write a query that, for each row in the table will return:
User_id, PrimaryEmail and Number of occurrences of that email address in the table. So I want the following table to be returned:
I know I need to use a sub query. So far I have:
select user_id, PrimaryEmail,
(select Count(PrimaryEmail) from table_contacts where PrimaryEmail = table_contacts.PrimaryEmail)
from table_contacts
But this is returning the count of all email addresses in the table.
What am I doing wrong?

The solution of Simone and Grażynka will group by address, so you will lose some row each time the email address is more than one time.
To display all row with a count of same email, you can do :
SELECT t1.user_id, t1.PrimaryEmail, (SELECT COUNT(*) FROM table_contacts t2 WHERE t2.PrimaryEmail = t1.PrimaryEmail) FROM table_contacts t1

try this:
select user_id, PrimaryEmail, Count(PrimaryEmail)
from table_contacts
group by PrimaryEmail
in SQL tryit editor a similar query would be
SELECT customerid,count(country),country FROM [Customers] group by country
but in this case you receive only the count of each email (one row for each email). Other (better) solutions have been proposed if you want to list all the rows with the couunt added.

Try this one:
Select user_id, primaryemail, count(*)
from table_contacts
group by user_id, primaryemail

You need a group by, not a subquery
something like
select user_id, PrimaryEmail, Count(PrimaryEmail)
from table_contacts
group by PrimaryEmail

This should do the job:
select t1.user_id, t1.PrimaryEmail, count(*)
from table_contacts t1
join table_contacts t2 on t1.PrimaryEmail = t2.PrimaryEmail
group by t1.user_id, t1.PrimaryEmail
order by t1.user_id;

Related

Count Distinct on multiple values within same column in SQL Aggregation

Objective:
I wanted to show the number of distinct IDs for any combination selected.
In the below example, I have data at a granular level: ID level data.
I wanted to show the number of distinct IDs for each combination.
For this, I use count distinct which will give me '1' for the below combinations.
But let's say if I wanted to find the number of IDs who made both E-commerce and Face to face transactions, in that case, if I just use this data, I would be showing the sum of E-comm and Face to face and the result would be '2' instead of '1'.
And this is not limited to Ecom/Face to face. I wanted to apply the same logic for all columns.
Please let me know if you have any other alternative approach to address this issue.
First aggregate in your table to get the distinct ids for each TranType:
SELECT TranType, COUNT(DISTINCT id) counter_distinct
FROM tablename
GROUP BY TranType
and then join to the table:
SELECT t.*, g.counter_distinct
FROM tablename t
INNER JOIN (
SELECT TranType, COUNT(DISTINCT id) counter_distinct
FROM tablename
GROUP BY TranType
) g ON g.TranType = t.TranType
Or use a correlated subquery:
SELECT t1.*,
(SELECT COUNT(DISTINCT t2.id) FROM tablename t2 WHERE t2.TranType = t1.TranType) counter_distinct
FROM tablename t1
But let's say if I wanted to find the number of IDs who made both E-commerce and Face to face transactions, in
You can get the list of ids using:
select id
from t
where tran_type in ('Ecomm', 'Face to face')
group by id
having count(distinct tran_type) = 2;
You can get the count using a subquery:
select count(*)
from (select id
from t
where tran_type in ('Ecomm', 'Face to face')
group by id
having count(distinct tran_type) = 2
) i;

Filter data on group by query

I've got my MySQL query below:
select * from messages
where id in (select max(id) from messages group by Name)
Why does this query not work?:
select * from messages
where id in (select max(id) from messages where field <> 'value' group by Name)
It would be helpful to know what you mean by "doesn't work" - for example, whether you get an error or whether it produces unexpected results. However, going by your comment, and assuming you define "last item sales" by the maximum ID per user, I would recommend JOINing your table to a subquery that selects the maximum ID per user. For example, given the sample sales data set mentioned in your comment, you could write a query like so:
select s.*
from sales s
join (
select user_name, max(id) as max_id
from sales
where sale_item <> 'Ship'
group by user_name
) q
on q.user_name = s.user_name
and q.max_id = s.id
I have created a SQL Fiddle demonstrating the output of the query.

SQL - Get all rows of table one and count of rows

How do I create an SQL query which gets all the rows of the table and count of rows inserted under e-mail?
I tried something like this, but this groups the rows and so I don't get all the rows.
SELECT *, COUNT(email) AS 'count' FROM adverts GROUP BY email
select a1.*, a2.count
from adverts a1
join
(
SELECT email, COUNT(*) AS 'count'
FROM adverts
GROUP BY email
) a2 on a1.email = a2.email
try this,
select *,
(select count(Email)
from adverts where adverts.Email =a.Email) as EmailCount
from adverts as a
or this
SELECT *, COUNT(email) OVER (PARTITION BY email) as EmailCount FROM adverts

MySQL look for duplicates on multiple fields

I have a MySQL database with the following fields:
id, email, first_name, last_name
I want to run an SQL query that will display rows where id and email exists more than once.
Basically, the id and email field should only have one row and I would like to run a query to see if there are any possible duplicates
If you just want to return the id and email that are duplicated, you can just use a GROUP BY query:
SELECT id, email
FROM yourtable
GROUP BY id, email
HAVING COUNT(*)>1
if you also want to return the full rows, then you have to join the previous query back:
SELECT yourtable.*
FROM
yourtable INNER JOIN (
SELECT id, email
FROM yourtable
GROUP BY id, email
HAVING COUNT(*)>1
) s
ON yourtable.id = s.id AND yourtable.email=s.email
You'll want something like this:
select field1,field2,field3, count(*)
from table_name
group by field1,field2,field3
having count(*) > 1
See also this question.
You can search for all ids that meet a specific count by grouping them and using a having clause like this:
SELECT id, COUNT(*) AS totalCount
FROM myTable
GROUP BY id
HAVING COUNT(*) > 1;
Anything this query returns has a duplicate. To check for duplicate emails, you can just change the column you're selecting.

MySql Select latest rows, then group, then count

I have a table with the following columns; id, post_id, status, and datetime. Every time a post(post_id) is updated, a row is inserted with the latest status and datetime timestamp. I am created a pie chart of by status. Therefore I must first SELECT the latest entry of a post (ignoring all past updates), then COUNT how many rows are returned and group by status. What does my query look like?
SELECT status, COUNT(*) AS statusCnt
FROM inspections
WHERE id IN (SELECT MAX(id) FROM inspections GROUP BY post_id)
GROUP BY status
Untested alternative:
SELECT i1.status, COUNT(i1.*) AS statusCnt
FROM inspections i1
JOIN (
SELECT MAX(i2.id) AS maxID FROM inspections i2 GROUP BY i2.post_id
) AS innerTbl ON i1.id = innerTbl.id
GROUP BY i1.status
SELECT count(*) FROM (SELECT *,MAX(time) FROM table GROUP BY post_id) As b
As per my interpretation of your question, query should be like this