Answer the question
In order to leave comments, you need to log in
Why is the postgres query not executing?
Good afternoon!
Used by PostgreSql as a base.
There are 3 tables: Users, Offers, Ratings. It is necessary that at first there was a sorting by ratings in descending order, then by the offered prices from the offer in ascending order. To have higher priority offers first:
Users:
--------------------
m_users
--------------------
id
name
--------------------
Offers:
--------------------
mс_offers
----- ---------------
id
user_id
price
--------------------
Ratings:
---------- ----------
ms_offers
--------------------
id
user_id
name
--------------------
I try this:
SELECT * FROM m_users
LEFT JOIN mc_rating ON m_users .id = mc_rating.user_id
LEFT JOIN mc_offers ON users.id = mc_offers.user_id
ORDER BY ( SUM(mc_rating.name) / COUNT(mc_rating.name) GROUP BY mc_rating.user_id ) DESC, mc_offers.price;
SELECT * FROM m_users
LEFT JOIN mc_rating ON m_users .id = mc_rating.user_id
LEFT JOIN mc_offers ON users.id = mc_offers.user_id
ORDER BY ( SUM(mc_rating.name) / COUNT(mc_rating.name)) DESC, mc_offers.price
GROUP BY mc_rating.user_id;
Answer the question
In order to leave comments, you need to log in
Try to list in GROUP BY all the fields that are involved in the query
So it turned out:
SELECT
m_users.*
FROM m_users
LEFT JOIN mc_rating ON m_users.id = mc_rating.user_id
LEFT JOIN mc_offers ON m_users.id = mc_offers.driver_id
GROUP BY mc_rating.user_id, m_users.id, mc_offers.price
ORDER BY ( SUM(mc_rating .name) / COUNT(mc_rating.name)) DESC, mc_offers.price;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question