Can I get the count results for particular field from table. for example im using this query,
select id,retailer,email from tab
i got the result set,
1 ret1 test1#test.com
2 ret2 test1#test.com
3 ret3 test1#test.com
4 ret1 test2#test.com
5 ret2 test2#test.com
6 ret6 test2#test.com
What I need is count of (test1#test.com) as 3 times like wise. thanks.
This will give you the count of all email addresses in that table:
SELECT email, COUNT(*) FROM tab GROUP BY email;
If you want to get only one particular one count use this:
SELECT COUNT(*) FROM tab WHERE email = 'test#example.com';
To count a single email:
select count(id)
from tab
where email = 'test1#test.com'
or to count all email values:
select email, count(email)
from tab
group by email
To group all of your emails together to count them:
SELECT email
, COUNT(*) AS 'count'
FROM `tab`
GROUP BY email
If you're looking for just a single email address:
SELECT email
, COUNT(*) AS 'count'
FROM `tab`
WHERE email = 'test#example.com'
Related
Please help me with a problem with this SQL in phpmyadmin that finds duplicate records for cleanup:
SELECT userid, MIN(logged_time),`logged_time`, count(email) as duplicated_count
FROM tbl_users
GROUP BY email HAVING count(email) > 1
ORDER BY `duplicated_count` DESC
Here is the GOAL:
Trying to list the duplicate email records in a user database that also tracks when they last used the system under the account.
Trying to identify the records with the oldest logged_time so those duplicates ONLY can be deleted.
PROBLEM:
The SQL above WORKS to identify the duplicates. AND, it WORKS to display the oldest (min) login. BUT>>> The one userID it returns is NOT the user id with the oldest login.
In other words, the data returned is from different records in the duplicate search for the same user email.
The results look like:
userid, MIN(logged_time), logged time,
"111" "2013-04-10 22:35:21", "2017-10-01 04:17:49"
SO...... User "111" is not the one I want to delete! That userid matches the most RECENT logged time. I want the userid for the record that matched MIN(logged_time).
Thanks for the help! I know this may be confusing.
MySQL version is 5.5, so the code posted doesn't seem to work with this version. Any other suggestions?
The one userID it returns is NOT the user id with the oldest login.
Selecting min(logged_time) does not say to only return the oldest login. It just gives you the oldest logged_time for each email.
What you asked for is invalid SQL, but MySQL allows it. It's invalid because it leads to exactly the trouble you're having.
Say we have this.
userid email logged_time
-----------------------------
1 email1 2021-01-01
2 email2 2021-01-01
3 email1 2021-02-01
4 email3 2021-01-01
If we select userid, min(logged_time), count(email) from tbl_users group by email, MySQL can only show you one row per email. For email2 and email3 that's fine, there's only one option. But for email1 it has to choose either 1 or 3.
Normally this would make the query invalid and you would get an error. You can't select a non-aggregated column (userid) which is not in the group by.
But MySQL allows this. It picks a userid at random. And we get the problem you're having.
userid min(logged_time) count(email)
--------------------------------------
2 2021-01-01 1
3 2021-01-01 2
4 2021-01-01 1
See MySQL Handling of GROUP BY for more, and I'd suggest you turn it off with ONLY_FULL_GROUP_BY.
What you're asking for is a bit tricky.
You need to both find emails with duplicates AND pick the oldest row for each email. You can't do all that in a single query, you need two queries joined together.
You know how to find duplicate emails.
-- Note that if you have two entries with the same userid and email
-- it will count that as a duplicate.
select email
from tbl_users
group by email
having count(email) > 1
To order the emails by their logged_time use the row_number window function.
select
userid,
email,
logged_time,
row_number() over(partition by email order by logged_time asc) as login_order
from tbl_users
That will return all the rows with each ranked.
userid email logged_time login_order
--------------------------------------------
1 email1 2021-01-01 1
2 email2 2021-01-01 1
3 email1 2021-02-01 2
4 email3 2021-01-01 1
Join them together and select only where login_order = 1.
-- Get the emails with dups.
with users_with_duplicates as (
select email
from tbl_users
group by email
having count(email) > 1
),
-- Get the logins by email ranked by ascending logged_time
users_in_login_order as (
select
userid,
email,
logged_time,
row_number() over(partition by email order by logged_time asc) as login_order
from tbl_users
)
select
users_in_login_order.*
from users_in_login_order
-- The join will constrain users_in_login_order to only emails with dups
join users_with_duplicates
on users_in_login_order.email = users_with_duplicates.email
where login_order = 1
Try it.
I have a table with this sample data:
place_id email
----------------------------
3 uno#uno.com
3 dos#dos.com
4 tres#tres.com
5 uno#uno.com
6 uno#uno.com
3 dos#dos.com
4 tres#tres.com
I want to show the emails that are in different places, I tried this query:
select email, count(email)
from table
group by email
having count(email) > 1
The problem is, this shows the duplicated rows in the same place, and I need to show only rows in different places. For example show only the email "uno#uno.com", that is in the places 3, 5 y 6, and no the "dos#dos.com" that is repeated in the same place.
Thanks.
If you only want the emails, you can use aggregation:
select email
from t
group by email
having min(place) <> max(place);
If you want the places as well in a unique list, you can do:
select distinct place, email
from t
where exists (select 1
from t t2
where t2.email = t.email and t2.place <> t.place
);
And, although you can use window functions, the solution is not as obvious:
select distinct place, email
from (select t.*,
min(t.place) over (partition by t.email) as min_place,
max(t.place) over (partition by t.email) as max_place
from t
) t
where min_place <> max_place;
You could use windowed COUNT(*) supported on modern RDBMS.
SELECT *
FROM (SELECT t.*, COUNT(DISTINCT place_id) OVER(PARTITION BY email) AS cnt
FROM tab t) sub
WHERE cnt > 1
DBFiddle Demo
SQL Sever/MariaDB/MySQL 8.0/PostgreSQL:
SELECT *
FROM (SELECT *, COUNT(*) OVER(PARTITION BY email) AS cnt
FROM (SELECT DISTINCT place_id, email FROM tab) s
)sub
WHERE cnt > 1;
DB-Fiddle.com Demo
You can use simple GROUP BY clause with HAVING clause to filter out the unique place
select place_id, email
from table t
group by place_id, email
having count(*) = 1;
Thanks a lot, i tried the first solution of Gordon Linoff and it's worked. But i have a little problem, i have a "where" clause, and this:
select email, count(email) from data where place = 2 or place=3 or place=4 group by email having min(place) <> max(place)
show the same results as:
select email, count(email) from data where place = 2 or place=3 group by email having min(place) <> max(place)
because it's an or condition, but i don't know how to repair and in the first query how to show only the items that are in all these places, not only in two of them.
I have a table with figures like this
Report used UserID
1 2
1 2
1 2
2 2
In this case I'm looking to count the 1's in the 'Report used' column, which would give me the value 3. I might find a few of these in this column for different users, so I'd want to count how many times I found 3 1's.
I've tried using SELECT COUNT to count specific numbers but I'm not sure how to count this count, if you follow me.
Try this:
SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid
Also check this:
SELECT COUNT(userid)
FROM (SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid) a
WHERE onescount = 3
If I've got it right:
select Report_used,RU_count,count(*)
from
(select Report_used, UserID, count(*) RU_Count
from t
group by Report_used, UserID) t1
group by Report_used,RU_count;
I have a column that takes user names. How can I count the number of instances of a users name. For example I have 10 rows and in column username i want to count all names that show up multiple times. I would like to build a list of the top contributors to my database. So if username alex shows up 5 times and jeff shows up 3 and april shows up 2 times i will count this and from that I can build my list.
Try GROUP BY:
SELECT username, COUNT(*) AS user_count
FROM yourtable
GROUP BY username
ORDER BY user_count DESC
Try something like
SELECT USER_NAME, COUNT(USER_NAME) FROM YOUR_TABLE GROUP BY USER_NAME;
If you want to get a count of all the usernames then you just do the following SQL:
Select Count(*) from tablename
If you want to get just the count of unique usernames
Select Count(*) from tablename Group by username
I want to display the user with the most posts. the posts are added by counting how many times their username is displayed in the database. How can I grab all the elements and check to see which value appears more then others?
So say my database looks like this:
id | username
1 | test
2 | test
3 | no test
"test" is shown the most, so how could I say
highest poster: "test"
This query returns username and number of occurrences, sorted in reverse order, so the first record is the one with more occurrences:
select username, count(id) from tablename
group by username
order by count(id) desc
UPDATE:
As pointed by thedugas and Joe Phillips, you can add a limit 1 clause to this query to get only the record with the highest number of occurrences
select username, count(id) as uc
from tableName
group by username
order by uc desc
limit 1
SELECT username
FROM mytable
GROUP BY username
ORDER BY COUNT(1) DESC
LIMIT 1