How to have SQL query with 2 subqueries divided - sql

I have a database which has these tables:
Users (id, email)
Trips (id, driver_id)
MatchedTrips (id, trip_id)
I need to get for each user the total number of trips he created divided by the total matches found.
I am stuck in building the raw SQL query for this. Here is what I tried, and sure it's far from being correct.
SELECT
users.email,
total_trips.count1 / total_matches.count2
FROM users CROSS JOIN (SELECT
users.email,
count(trips.driver_id) AS count1
FROM trips
INNER JOIN users ON trips.driver_id = users.id
GROUP BY users.email) total_trips
CROSS JOIN (SELECT users.email, count(matches.trip_id) AS count2
FROM matches
LEFT JOIN trips ON matches.trip_id = trips.id
LEFT JOIN users ON trips.driver_id = users.id
GROUP BY users.email) total_matches;

You can calculate total trips and total matches for each driver in the way like this:
select driver_id, count(t.id) as total_trips, count(m.id) as total_matches
from trips t
left join matches m on (t.id = trip_id)
group by 1
Use this query as a derived table in join with users:
select email, total_trips, total_matches, total_trips::dec/ nullif(total_matches, 0) result
from users u
left join (
select driver_id, count(t.id) as total_trips, count(m.id) as total_matches
from trips t
left join matches m on (t.id = trip_id)
group by 1
) s on u.id = driver_id
order by 1;
SQLFiddle.

The simplest way is probably to use count(distinct):
select u.email,
count(distinct t.id) as num_trips,
count(distinct m.id) as num_matches,
(count(distinct t.id) / count(distinct m.id)) as ratio
from users u left join
trips t
on t.driver_id = u.id left join
matches m
on m.trip_id = t.trip_id
group by u.email;
Note: If emails are unique, then the query can be simplified. count(distinct) can be expensive under some circumstances.

Related

SQL Sum and Count JOIN Multiple tables

Please help, I want to merge this two query
I have 3 Tables (places,ratings,places_image)
places
id
name
description
ratings
id
rating
place_id
user_id
places_image
id
place_id
image
These are the 2 queries:
SELECT places.*, SUM(rating) AS total_rating,COUNT(ratings.user_id) AS total_user FROM ratings, places WHERE ratings.place_id = places.id GROUP BY places.id
SELECT places.*, places_images.image FROM places, places_images WHERE places.id = places_images.place_id GROUP BY places.id
Query 1
Query 2
I tried to do this query but it give duplicate data for the aggregate function
SELECT places.id, places.name, places.description, places_images.image ,SUM(rating) AS total_rating,COUNT(ratings.user_id) AS total_user FROM places_images JOIN places ON places_images.place_id = places.id JOIN ratings ON ratings.place_id = places.id GROUP BY places.id
Query 3
How can i combine it ?
You need to use 2 Inner Join on the place_id and user_id
It'll be something like
SELECT SUM(r.rating) AS total_ratings, COUNT(r.user_id) AS total_users
FROM ratings r
INNER JOIN places p ON p.id = r.user_id
INNER JOIN places_image pi ON pi.place_id = r.place_id
GROUP BY r.id
Seems the only thing i need is to subquery it
select a.*, d.image, b.totalRating, c.totalUser from places a, ( select place_id, sum(rating) AS totalRating from ratings group by place_id ) b, ( select place_id, count(id) AS totalUser from ratings group by place_id ) c, places_images d where c.place_id = a.id and b.place_id = a.id and d.place_id = a.id GROUP BY a.id
Result
Thank you very much, GBU

Select only those users who have the most visits to provided district

I have a query that selects users with the districts which they visited and visits count.
select users.id, places.district, count(users.id) as counts from users
left join visits on users.id = visits.user_id
inner join places on visits.place_id = places.id
group by users.id, places.district
I need to select only those users who have visited provided district the most. For example, I have a user with id 1 who visited district A one time and district B three times. If I provide district B as parameter, user 1 will be in select. If I want to select users from district A, user 1 will not be in select.
I think that's ranking, then filtering:
select *
from (
select u.id, p.district, count(*) as cnt_visits,
rank() over(partition by u.id order by count(*) desc)
from users u
inner join visits v on u.id = v.user_id
inner join places p on p.id = v.place_id
group by u.id, p.district
) t
where rn = 1 and district = ?
Note that you don't actually need table users to get this result. We could simplify the query as:
select *
from (
select v.user_id, p.district, count(*) as cnt_visits,
rank() over(partition by u.id order by count(*) desc)
from visits v
inner join places p on p.id = v.place_id
group by v.user_id, p.district
) t
where rn = 1 and district = ?
This query handles top ties: if a user had the same, maximum number of visits in two different districts, both are taken into account. If you don't need that feature, then we can simplify the subquery with distinct on:
select *
from (
select distinct on (v.user_id) v.user_id, p.district, count(*) as cnt_visits
from visits v
inner join places p on p.id = v.place_id
group by v.user_id, p.district
order by v.user_id, cnt_visits desc
) t
where district = ?

Issue with getting the rank of a user based on combined columns in a join table

I have a users table and each user has flights in a flights table. Each flight has a departure and an arrival airport relationship within an airports table. What I need to do is count up the unique airports across both departure and arrival columns (flights.departure_airport_id and flights.arrival_airport_id) for each user, and then assign them a rank via dense_rank and then retrieve the rank for a given user id.
Basically, I need to order all users according to how many unique airports they have flown to or from and then get the rank for a certain user.
Here's what I have so far:
SELECT u.rank FROM (
SELECT
users.id,
dense_rank () OVER (ORDER BY count(DISTINCT (flights.departure_airport_id, flights.arrival_airport_id)) DESC) AS rank
FROM users
LEFT JOIN flights ON users.id = flights.user_id
GROUP BY users.id
) AS u WHERE u.id = 'uuid';
This works, but does not actually return the desired result as count(DISTINCT (flights.departure_airport_id, flights.arrival_airport_id)) counts the combined airport ids and not each unique airport id separately. That's how I understand it works, anyway... I'm guessing that I somehow need to use a UNION join on the airport id columns but can't figure out how to do that.
I'm on Postgres 13.0.
I would recommend a lateral join to unpivot, then aggregation and ranking:
select *
from (
select f.user_id,
dense_rank() over(order by count(distinct a.airport_id) desc) rn
from flights f
cross join lateral (values
(f.departure_airport_id), (f.arrival_airport_id)
) a(airport_id)
group by f.user_id
) t
where user_id = 'uuid'
You don't really need the users table for what you want, unless you do want to allow users without any flight (they would all have the same, highest rank). If so:
select *
from (
select u.id,
dense_rank() over(order by count(distinct a.airport_id) desc) rn
from users u
left join flights f on f.user_id = u.id
left join lateral (values
(f.departure_airport_id), (f.arrival_airport_id)
) a(airport_id) on true
group by u.id
) t
where id = 'uuid'
You're counting the distinct pairs of (departure_airport_id, arrival_airpot_id). As you suggested, you could use union to get a single column of airport IDs (regardless of whether they are departure or arrival airports), and then apply a count on them:
SELECT user_id, DENSE_RANK() OVER (ORDER BY cnt DESC) AS user_rank
FROM (SELECT u.id AS user_id, COALESCE(cnt, 0) AS cnt
FROM users u
LEFT JOIN (SELECT user_id, COUNT DISTINCT(airport_id) AS cnt
FROM (SELECT user_id, departure_airport_id AS airport_id
FROM flights
UNION
SELECT user_id, arrival_airport_id AS airport_id
FROM flights) x
GROUP BY u.id) f ON u.id = f.user_id) t

Query to find the max of a value with multiple group by attributes

I am trying to get the max bidamt on a particular item and display the user who has bid that amount
I am able to get the results in two different queries each returning a subset of what I want
select username, auctionbids.itemid, description, bidamt from AuctionBids
inner join users on auctionbids.userid = users.id
inner join auctionitems on auctionbids.itemid = auctionitems.itemid
select ab.itemid,max(ab.bidamt) as bidmax from auctionbids as ab group by ab.itemid
I want to get username, itemid, item descripton, max(bidamt) joining three tables - users, auctionitems, auctionbids.
users contains userid and username
auctionitems contains itemid and item description
auctionbids contains userid, itemid, bidamt
The following query should do what you want:
SELECT a.item_description,a.username,a.bidamt FROM
(
SELECT au.item_description,u.username,ab.bidamt, RANK() OVER(PARTITION BY au.itemid ORDER BY ISNULL(ab.bidamt,0) DESC) AS [Rank]
FROM auctionitems au
LEFT JOIN auctionbids ab ON au.itemid = ab.itemid
LEFT JOIN users u ON ab.userid = u.userid ) a WHERE [Rank] = 1
As you said, the two different queries each returning a subset of what you want, your query should like this:
SELECT am.username, am.itemid, am.descripton, max(am.bidamt) AS bidmax
FROM (
SELECT username, auctionbids.itemid, auctionitems.description, bidamt FROM AuctionBids
INNER JOIN users ON auctionbids.userid = users.id
INNER JOIN auctionitems ON auctionbids.itemid = auctionitems.itemid
) AS am
GROUP BY am.itemid
Without example data, it is impossible to verify this works correctly. But you will need to match the bidamt with the MAX bidamt for a particular item.
SELECT DISTINCT
users.username,
auctionbids.itemid,
auctionitems.description,
auctionbids.bidamt
FROM
users
INNER JOIN auctionbids on users.userid = auctionbids.userid
INNER JOIN auctionitems on auctionbids.itemid = auctionitems.itemid
WHERE
auctionbids.bidamt = (SELECT MAX(bidamt) OVER (PARTITION BY itemid, description))
AND
itemid = < whatever you want >
Thanks for the ideas which led to this query -
SELECT am.*, auctionbids.submitted, users.username, auctionitems.description
FROM
(SELECT ab.itemid, max(ab.bidamt) as bidmax
FROM
auctionbids ab
GROUP BY ab.itemid) AS am
INNER JOIN auctionbids ON am.itemid = auctionbids.itemid and am.bidmax = auctionbids.bidamt
INNER JOIN users ON auctionbids.userid = users.id
INNER JOIN auctionitems ON auctionbids.itemid = auctionitems.itemid
So basically the max has to be found with a group by on itemid and then the inner join with rest of the tables to fetch the attributes.

Writing a Mathematical Formula in SQL?

I have these tables: users, comments, ratings, and items
I would like to know if it is possible to write SQL query that basically does this:
user_id is in each table. I'd like a SQL query to count each occurrence in each table (except users of course). BUT, I want some tables to carry more weight than the others. Then I want to tally up a "score".
Here is an example:
user_id 5 occurs...
2 times in items;
5 times in comments;
11 times in ratings.
I want a formula/point system that totals something like this:
items 2 x 5 = 10;
comments 5 x 1 = 5;
ratings 11 x .5 = 5.5
TOTAL 21.5
This is what I have so far.....
SELECT u.users
COUNT(*) r.user_id
COUNT(*) c.user_id
COUNT(*) i.user_id
FROM users as u
JOIN COMMENTS as c
ON u.user_id = c_user_id
JOIN RATINGS as r
ON r.user_id = u.user_id
JOIN ITEMS as i
i.user_id = u.user_id
WHERE
????
GROUP BY u.user_id
ORDER by total DESC
I am not sure how to do the mathematical formula portion (if possible). Or how to tally up a total.
Final Code based on John Woo's Answer!
$sql = mysql_query("
SELECT u.username,
(a.totalCount * 5) +
(b.totalCount) +
(c.totalCount * .2) totalScore
FROM users u
LEFT JOIN
(
SELECT user_id, COUNT(user_id) totalCount
FROM items
GROUP BY user_id
) a ON a.user_id= u.user_id
LEFT JOIN
(
SELECT user_id, COUNT(user_id) totalCount
FROM comments
GROUP BY user_id
) b ON b.user_id= u.user_id
LEFT JOIN
(
SELECT user_id, COUNT(user_id) totalCount
FROM ratings
GROUP BY user_id
) c ON c.user_id = u.user_id
ORDER BY totalScore DESC LIMIT 10;");
Maybe this can help you,
SELECT u.user_ID,
(a.totalCount * 5) +
(b.totalCount) +
(c.totalCount * .2) totalScore
FROM users u LEFT JOIN
(
SELECT user_ID, COUNT(user_ID) totalCount
FROM items
GROUP BY user_ID
) a ON a.user_ID = u.user_ID
LEFT JOIN
(
SELECT user_ID, COUNT(user_ID) totalCount
FROM comments
GROUP BY user_ID
) b ON b.user_ID = u.user_ID
LEFT JOIN
(
SELECT user_ID, COUNT(user_ID) totalCount
FROM ratings
GROUP BY user_ID
) c ON c.user_ID = u.user_ID
ORDER BY totalScore DESC
but based on yur query above,thismay also work
SELECT u.users
(COUNT(*) * .5) +
COUNT(*) +
(COUNT(*) * 2) totalcore
FROM users as u
LEFT JOIN COMMENTS as c
ON u.user_id = c_user_id
LEFT JOIN RATINGS as r
ON r.user_id = u.user_id
LEFT JOIN ITEMS as i
ON i.user_id = u.user_id
GROUP BY u.user_id
ORDER by totalcore DESC
The only difference is by using LEFT JOIN. You will not use INNER JOIN in this situation because there are chances that user_id is not guaranteed to exists on every table.
Hope this makes sense
Here's an alternative approach:
SELECT
u.user_id,
SUM(s.weight) AS totalScore
FROM users u
LEFT JOIN (
SELECT user_id, 5.0 AS weight
FROM items
UNION ALL
SELECT user_id, 1.0
FROM comments
UNION ALL
SELECT user_id, 0.5
FROM ratings
) s
ON u.user_id = s.user_id
GROUP BY
u.user_id
I.e. for every occurrence of every user in every table, a row with a specific weight is produced. The UNIONed set of weights is then joined to the users table for subsequent grouping and aggregating.