How to select all users who made more than 10 submissions - sql

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

Related

How to Join only first row, disregard further matches

I have 2 tables
Table Users:
UserID | Name
Table Cars:
CarID | Car Name | FK_UserID
A user can have more than 1 car.
I want to join each user with 1 car only, not more.
Having looked at other threads here,
I've tried the following:
Select users.UserID, users.name, carid
from Users
join cars
on users.UserID =
(
select top 1 UserID
from users
where UserID = CarID
)
But it still returns more than 1 match for each user.
What am I doing wrong?
You can try like below using ROW_NUMBER() function
select userid, username, carname
from
(
Select users.UserID as userid,
users.name as username,
cars.carname as carname,
ROW_NUMBER() OVER(PARTITION BY users.UserID ORDER BY users.UserID) AS r
from Users
join cars
on users.UserID = cars.FK_UserID
) XXX
where r = 1;
with x as
(select row_number() over(partition by userid order by carid) as rn,
* from cars)
select u.userid, x.carid, x.carname
from users u join x on x.userid = u.userid
where x.rn = 1;
This is one way to do it using row_number function.
Another way to do it
select u.UserID,
u.name,
(select TOP 1 carid
from cars c
where u.UserID = c.FK_UserID
order by carid) carid -- Could be ordered by anything
from Users u
-- where only required if you only want users with cars
where exists (select * from car c where u.UserID = c.FK_UserID)
Best would be to do a subquery and use a group-by in it to return only 1 user and a car for each user. Then join that to the outer user table.
Here is an example:
select *
from user_table u
join (
select userid
, max(carname)
from cars
group by userid
) x on x.userId = u.userId
or you could use the row_number() examples above if you want a specific order (either this example or theirs will do the trick)

SQL - Select all rows that have the same Email but different Name

I'm trying to select all the users in a table that have the same Email but have a different Name. So far I have managed to get all the rows that have duplicate Email but I'm stuck on the next step.
SELECT * FROM users WHERE Email IN
(SELECT Email FROM users GROUP BY Email HAVING COUNT(*) > 1)
Thanks in advance
I think you just want count(distinct name) in the subquery:
SELECT *
FROM users
WHERE Email IN (SELECT Email
FROM users
GROUP BY Email
HAVING COUNT(distinct Name) > 1
) ;
I prefer having min(name) <> max(name) for the having clause. It is slightly more efficient.
However, the most efficient method is probably to use window functions:
select u.*
from (select u.*, min(name) over (partition by email) as minname,
max(name) over partition by email) as maxname
from users u
) u
where minname <> maxname;
Try this
SELECT *
FROM users U1
INNER JOIN
users U2
on U1.Email=U2.Email
AND U1.Name <> U2.Name
You can try the following:
SELECT u.*
FROM users u
LEFT JOIN (
SELECT Email
FROM users
GROUP BY Email
HAVING COUNT(*) = COUNT(DISTINCT name)
) tmp ON u.Email = tmp.Email
WHERE tmp.Email IS NOT NULL

SQL: Comparing MAX Dates from Two different Tables

I have 3 Tables
User
Attendence
Payment
Now I like to get
GroupID, UserName, MAX(PaymetDate), MAX(AttendenceDate)
Where MAX(PaymetDate) IS LESS THAN MAX(AttendenceDate)
This what I have Tried
SELECT MAX(PaymetDate) AS Paied_Upto
FROM Payment
Group by GroupID
SELECT MAX(AttendenceDate) AS Last_ AttendenceDate
FROM Attendence FULL OUTER JOIN Users ON Attendence.Username = Users.Username
Group by Users.GroupID
But how do get them to work together?
Thank
Try this:
SELECT u.GroupID, u.UserName, py.LastPaymentDate, at.LastAttendenceDate
FROM User AS u,
(SELECT Username, Max(AttendenceDate) AS LastAttendenceDate FROM Attendence GROUP BY Username) AS at,
(SELECT GroupID, Max(PaymetDate) AS LastPaymentDate FROM Payment GROUP BY GroupID) AS py
WHERE u.UserName=at.Username
AND u.GroupID=py.GroupID
AND py.LastPaymentDate < at.LastAttendenceDate;
try this
select p.GroupID, u.UserName, MAX(p.PaymetDate), MAX(a.AttendenceDate)
from dbo.Users u
inner join dbo.Attandence a
ON u.UserName = a.UserName
Inner join dbo.Payment p
ON u.groupID = p.GroupID
GROUP BY p.GroupID, u.UserName
Having MAX(p.PaymentDate) < MAX(a.attendenceDate)
I think this does what you need (SqlFiddle link):
select UserName, GroupID, MaxAttendanceDate, MaxPaymentDate
from (
select
u.UserName,
u.GroupID,
(select max(AttendanceDate)
from Attendance a
where a.UserName = u.UserName) as MaxAttendanceDate,
(select max(PaymentDate)
from Payment p
where p.GroupID = u.GroupId) as MaxPaymentDate
from [User] u
) x
where MaxAttendanceDate > MaxPaymentDate

MySQL: Find duplicate users WHERE item count < 1

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 ]

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.