I have a user table with id column and request table with user_id and session_id columns.
I want to get the number of sessions each user has.
Here is the query I wrote using a subquery:
select user.id,
(
select count(distinct session_id)
from request
where request.user_id = user.id
) as session_count
from user
What would be the join equivalent of this?
What about this?
select u.id, count(distinct r.session_id)
from user u
join request r on r.user_id = u.id
group by u.id
You can use GROUP BY without need a subquery such as
SELECT u.id, COUNT(DISTINCT r.session_id)
FROM user u
JOIN request r
ON u.id = r.user_id
GROUP BY u.id
Related
I have 4 tables
All ID related things are ints and the rest are texts.
I want to count the number of albums the user is tagged at so if a user is tagged in album1 once album2 once and album3 once it will show 3 and if more in any of them it will still show 3.
I tried to do:
SELECT COUNT(DISTINCT ALBUM_ID) FROM PICTURES WHERE ID=(SELECT PICTURE_ID FROM TAGS WHERE USER_ID=userId);
But this returned 1 although it was supposed to return 3 and the same happened without DISTINCT.
How can I get the amount?
EDIT:
I want to check only one user(I have the user's ID and name)
You must join users with LEFT joins to tags and pictures and aggregate:
SELECT u.id, u.name, COUNT(DISTINCT p.album_id) counter
FROM users u
LEFT JOIN tags t ON t.user_id = u.id
LEFT JOIN pictures p ON p.id = t.picture_id
GROUP BY u.id, u.name
If you want the result for a specific user only:
SELECT u.id, u.name, COUNT(DISTINCT p.album_id) counter
FROM users u
LEFT JOIN tags t ON t.user_id = u.id
LEFT JOIN pictures p ON p.id = t.picture_id
WHERE u.id = ?
GROUP BY u.id, u.name -- you may omit this line, because SQLite allows it
Or with a correlated subquery:
SELECT u.id, u.name,
(
SELECT COUNT(DISTINCT p.album_id)
FROM tags t INNER JOIN pictures p
ON p.id = t.picture_id
WHERE t.user_id = u.id
) counter
FROM users u
WHERE u.id = ?
Replace ? with the id of the user that you want.
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
I have 3 tables user, session and log. The user table stores all user relevant information while the session just connects the user with the log. And i want to get a list of all users with the latest log entry. The table design looks like this:
user (id, name, ...)
session (id, user_id)
log (id, session_id, time, type, ...)
My current query looks like this
SELECT *
FROM USER AS u
INNER JOIN session AS s
ON u.id = s.user_id
INNER JOIN log AS l
ON l.session_id = s.id
ORDER BY l.time DESC
But it's not hard to imagine that this just returns the data of all 3 tables sorted by date. How do i achieve a result that i just get every user just once with the data from the latest log entry ordered by the time of log (desc)?
Thanks in advance for your help.
You can use DISTINCT ON in conjunction with ORDER BY to get the latest row per user by log date. This will allow you to select the additional fields you need:
SELECT DISTINCT ON (u.id)
u.id,
u.Name,
l.type,
l.time
FROM user AS u
INNER JOIN session AS s ON u.id = s.user_id
INNER JOIN log AS l ON l.session_id = s.id
ORDER BY u.id, l.time DESC;
N.B. I don't know exactly what columns you need, but I have added a couple in to demonstrate as I don't like to advocate the use of SELECT *
For completeness there are a couple of other ways to achieve this, the first is to select the max in a subquery and join back to the outer query on both user_id and time:
SELECT u.id,
u.Name,
l.type,
l.time
FROM user AS u
INNER JOIN session AS s
ON u.id = s.user_id
INNER JOIN log AS l
ON l.session_id = s.id
INNER JOIN
( SELECT s.user_id, MAX(l.time) AS time
FROM session AS s
INNER JOIN log AS l
ON l.session_id = s.id
GROUP BY s.user_id
) AS MaxLog
ON MaxLog.user_id = u.id
AND MaxLog.time = l.time
ORDER BY l.time DESC;
Or you can use ROW_NUMBER():
SELECT id, Name, type, time
FROM ( SELECT u.id,
u.Name,
l.type,
l.time,
ROW_NUMBER() OVER(PARTITION BY u.id ORDER BY l.time DESC) AS RowNumber
FROM user AS u
INNER JOIN session AS s
ON u.id = s.user_id
INNER JOIN log AS l
ON l.session_id = s.id
) u
WHERE RowNumber = 1;
I've assumed some schema (user.user_name?), but you can do this by grouping and an aggregate like Max:
SELECT u.user_id,
u.user_name,
Max(l.time) AS LastLogTime
FROM USER AS u
LEFT JOIN session AS s
ON u.id = s.user_id
INNER JOIN log AS l
ON l.session_id = s.id
GROUP BY u.user_id,
u.user_name;
You won't be able to select * as we need to use GROUP BY
Similarly, ORDER BY l.time isn't applicable any more - you could still order by e.g. user_name
I've also LEFT JOINED - this way, if the user has no sessions, it will still return a record for the user, possibly with a LastLogTime of NULL.
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.
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