E
E
EVOSandru62015-08-25 08:13:52
PostgreSQL
EVOSandru6, 2015-08-25 08:13:52

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;

I get this error:
ERROR: syntax error at or near "GROUP"
LINE 28: ... BY ( SUM(mc_rating.name) / COUNT(mc_rating.name) GROUP BY ^
********** Error * *********
ERROR: syntax error at or near "GROUP"
SQL state: 42601
Symbol: 1213

Trying 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)) DESC, mc_offers.price 
GROUP BY mc_rating.user_id;

I catch this error:
ERROR: syntax error at or near "GROUP"
LINE 29: GROUP BY mc_rating.user_id;
^
********** Error **********
ERROR: syntax error at or near "GROUP"
SQL Status: 42601
Character: 1236

What could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Swartalf, 2015-08-25
@EVOSandru6

Try to list in GROUP BY all the fields that are involved in the query

E
EVOSandru6, 2015-08-25
@EVOSandru6

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 question

Ask a Question

731 491 924 answers to any question