I
I
IvanN7772015-12-28 09:58:07
PostgreSQL
IvanN777, 2015-12-28 09:58:07

Postgreas unique and order by conflict, what's wrong?

The expression of the form
.joins(:user => :profile).order_site_rating
gives duplicates, how and why I have not figured it out yet, if you tell me, I will also be glad.
The problem is the following
PG conflicts if we just add distinct

joins(:user => :profile).order_site_rating.distinct

Asks to add an expression from there to select and everything would be fine if
SELECT DISTINCT(users.id), users.*, profiles.first_name FROM users LEFT JOIN profiles ON users.id = profiles.user_id ORDER BY profiles.first_name

But unfortunately the selection is very difficult.
order('case when profiles.avatar is null then -1 else 1 end desc')

How can I add this to the select or how to fix the links so that there are no duplicates in the selection?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IvanN777, 2015-12-28
@IvanN777

ERROR: in a SELECT DISTINCT construct, the ORDER BY expressions must be in the select list
LINE 1: ...("anketa_owners"."town_id" IS NOT NULL) ORDER BY case when ...
^
: SELECT DISTINCT "anketa_owners".* FROM " anketa_owners" INNER JOIN "users" ON "users"."id" = "anketa_owners"."user_id" INNER JOIN "profiles" ON "profiles"."user_id" = "users"."id" WHERE ("anketa_owners". "town_id" IS NOT NULL) ORDER BY case when profiles.avatar is null then -1 else 1 end desc, profiles.rating DESC LIMIT 10 OFFSET 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question