MySQL: Find duplicate users WHERE item count < 1 - sql

Trying to find duplicate users by email, who have an item count less than 0. I've got the duplicate users working (though it returns the full list of users sorted, instead of a subset):
select users.id, email, count(email) as count
from users
group by users.email
order by count desc
I'm trying to join on Items where count(items.id) < 1, but that doesn't seem to work.
select users.id, email, count(email) as count
from users
join items on items.user_id = users.id
having count(items.id) < 1
group by users.email
order by count desc
I also tried an IN query, but can't seem to get the syntax right.
Easy way to do this? Thanks!
UPDATE:
This query did the trick:
select DISTINCT(u1.id), u1.email
from users u1
inner join users u2
on 1=1
and u1.email = u2.email
and u1.id != u2.id
where not exists (select * from items i where i.user_id = u1.id)
order by u1.id

Duplicated users:
select
email,
count(*) as count,
min(id) first_user_with_this_email_id
from users
group by email
having count(*) > 1
For second one, try this:
select
users.email,
count(*) as count
from users
left join items
on (items.user_id = users.id)
where items.id is null --there are no related items
group by users.email
having count(*) > 1 --there are at least two users
Another version of second:
select
u.email,
count(*) as count
from users u
where not exists (select * from items i where i.user_id = u.id)
group by u.email
having count(*) > 1 --there are at least two users
Make sure you have index on user_id in items table.

try using WHERE instead of "having".
May be this link helps: http://www.devx.com/DevX/Tip/21295
Modified query would be :
select users.id, email, count(email) as count
from users
join items on items.user_id = users.id
where count(items.id) < 1
group by users.email
order by count desc
[ did not run and check the query, just a correction ]

Related

Only show users that having cars more than one

Dear all I have users table and cars table.
and I have following join query:
select
users.id as user_id,
users.username,
users.job,
cars.id,
cars.brand as car_brand
FROM users
LEFT JOIN cars on users.id = cars.user_id
GROUP BY users.username, users.id, cars.id;
Here is the snapshot:
How to query for users that having cars more than one?
I tried code below but it return empty data:
How to get users that having more than one cars? (username: Ismed)
You can do in this was as well.
select
users.id as user_id,
users.username,
users.job,
cars.id,
cars.brand as car_brand
FROM users
LEFT JOIN cars on users.id = cars.user_id
where exists (select username, count(*) multiplecars
FROM users u
JOIN cars c on u.id = c.user_id
where users.username = u.username
group by
u.username
having count(*) > 1 )
If the users have more than one car (even if same brand then this will bring those records) if you only want users with more than one branded care you can do count(distinct)
The simplest and probably most performance method is to use window functions:
select user_id, username, job, id, brand
from (select u.id as user_id, u.username, u.job,
c.id, c.brand as car_brand,
count(*) over (partition by u.id) as num_cars
from users u join
cars c
on u.id = c.user_id
) uc
where num_cars > 1;
Note that I changed the left join to a join. If you have two matches, you are requiring a match. I also introduced table aliases so the query is easier to write and to read.
SELECT users.username
FROM users
WHERE users.id IN(
select
users.id
FROM users
JOIN cars on users.id = cars.user_id
GROUP BY users.id
HAVING COUNT(*) > 1
);
Filter users first who has more then one car then get corresponding details

SQL join 3 tables while getting count from 2 of them

Back with another SQL question about joins. I have 3 tables:
user: id, username, name, city, state, private
rides: id, creator, title, datetime, city, state
posts: id, title, user, date, state, city
I need to get the users from the user table, and based on the id of user, get the number of posts and rides for each person. Such as, user with id 25 has 2 rides and 4 posts, while the user with id 27 has 2 rides and 2 posts. The problem I am having, is that both users are coming back with 4 posts and rides each.
user.id = rides.creator = posts.user //just so you know what fields equals the user id
Here is my code:
select u.id, u.username, u.state, u.city, count(p.id) as TotalPosts, count(r.id) as TotalRides
from user u
left join posts p on p.user=u.id
left join rides r on r.creator=u.id
where private='public'
group by u.id
order by u.username, u.state asc;
If I separate them out, and just join the posts or the rides, I get the correct totals back. I tried switching the order of the joins, but I got the same results. Not sure what is going on.
Any ideas or thoughts are appreciated.
Your problem is a Cartesian product along two different dimensions. The best solution is to pre-aggregate the data:
select u.id, u.username, u.state, u.city, p.TotalPosts, r.TotalRides
from user u left join
(select user, count(*) as totalposts
from posts p
group by user
) p
on p.user = u.id left join
(select creator, count(*) as totalrides
from rides r
group by creator
) r
on r.creator = u.id
where u.private = 'public'
group by u.id
order by u.username, u.state asc;
you can always use a sub select.
select u.*,
(select count(*) from posts where user = u.id) as 'posts',
(select count(*) from rides where creator = u.id) as 'rides'
from users u
where .....

SQL: Finding user with most number of comments

I need to find out the user who has posted the most number of comments. There are two tables 1)users(Id, DisplayName) 2)comments(Id, UserId, test) . I have used the following query
Select DisplayName from users INNER JOIN (Select UserId, max(comment_count) as `max_comments from (Select UserId, count(Id) as comment_count from comments group by UserId) as` T1) as T2 ON users.Id=T2.UserId
However, this returns to me the Display Name of the user with Id = 1 rather than what I want. How do I work around this ?
SELECT TOP 1
U.DisplayName,
COUNT(C.ID) AS CommentCount
FROM
Users AS U
INNER JOIN Comments AS C ON U.ID = C.UserID
GROUP BY
U.DisplayName
ORDER BY
COUNT(C.ID) DESC

postgresql database

i wanna to make a query that select users that have same username and same hour of creation date by using postgresql database
Something like this should do the trick. This will return any user/hour pair along with the count (untested):
select users.username, datepart('hour', users.created_at), count(*) from users
inner join users u2
on users.username = u2.username
and datepart('hour', users.created_at) = datepart('hour', u2.created_at)
group by users.username, datepart('hour', users.created_at) having count(*) > 1
select u.*
from users u
join (
select username, date_trunc('hour', creation_timestamp)
from users
group by 1, 2
having count(*) > 1
) as x on u.username = x.username
order by u.username;
Should work nicely.

How to select all users who made more than 10 submissions

I have a submission table that is very simple: userId, submissionGuid
I want to select the username (simple inner join to get it) of all the users who have more than 10 submissions in the table.
I would do this with embedded queries and a group by to count submissions... but is there a better way of doing it (without embedded queries)?
Thanks!
This is the simplest way, I believe:
select userId
from submission
group by userId
having count(submissionGuid) > 10
select userId, count(*)
from submissions
having count(*) > 10
group by userId
SELECT
username
FROM
usertable
JOIN submissions
ON usertable.userid = submissions.userid
GROUP BY
usertable.username
HAVING
Count(*) > 1
*Assuming that your "Users" table is call usertable and that it has a column called "UserName"
I think the correct query is this (SQL Server):
SELECT s.userId, u.userName
FROM submission s INNER JOIN users u on u.userId = s.userId
GROUP BY s.userId, u.username
HAVING COUNT(submissionGuid) > 10
If you don't have the HAVING clause:
SELECT u.userId, u.userName
FROM users u INNER JOIN (
SELECT userId, COUNT(submissionGuid) AS cnt
FROM submission
GROUP BY userId ) sc ON sc.userId = u.userId
WHERE sc.cnt > 10
select userid, count(submissionGUID) as submitCount
from Submissions
group by userid, submitCount
having submitCount > 10