Answer the question
In order to leave comments, you need to log in
Postgre Sql DISTINCT array with ORDER BY?
It is necessary to select unique comments from orders and display the first 3 words, sorted by 2 words.
Here's what I got
select DISTINCT (regexp_split_to_array(commend, E'\\s+'))[:3] from orders ORDER BY (regexp_split_to_array(commend, E'\\s+'))[2];
ERROR: in SELECT DISTINCT clause, ORDER BY expressions must be in the select list
Answer the question
In order to leave comments, you need to log in
select * from (
select DISTINCT (regexp_split_to_array(commend, E'\\s+'))[:3] as words from orders
) t
order by words[2]
DISTINCT is actually a private form of GROUP BY. Therefore, it includes all restrictions for GROUP BY regarding the use of data source fields in window functions, post-selection and sorting expressions.
Therefore, use the output set field for sorting.
SELECT DISTINCT (regexp_split_to_array(commend, E'\\s+'))[:3] AS first_3_words
FROM orders
ORDER BY first_3_words[2];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question