K
K
Ken Jee2016-10-04 12:50:23
PostgreSQL
Ken Jee, 2016-10-04 12:50:23

How to sort by field in PostgreSQL using DISTINCT?

There is a request:

SELECT DISTINCT field_1, field_2 FROM table_1 ORDER BY field_3

It ends with an error:
SQLSTATE[42P10]: Invalid column reference: 7 ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list

As I understand the essence of the problem is that sorting can only be done by the field that is listed in DISTINCT. Can this be bypassed somehow?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Oparin, 2016-10-04
@Machez

And how do you imagine sorting by a field that is missing?
Here you have a sign

name   value
aaa    111
aaa    222
bbb    156

Are you doing
in what order should the arrangement of aaa and bbb happen? which is greater than {111; 222} or {156}?

R
romy4, 2016-10-04
@romy4

To sort by a field, you must first select a field, but you do not have
it
selected

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question