1

Hello i want to query a table and make a select distinct on but with different ordey by and i want this with desc. How can i do this?

CREATE TABLE test_dupl2(id SERIAL, letter_one TEXT, number_int INT, primary key (id));

INSERT INTO test_dupl2(letter_one,number_int) VALUES ('A',1), ('A',2), ('B',1), ('A', 9), ('B', 4);

My query

select letter_one, number_int from
    (SELECT DISTINCT ON (letter_one) letter_one, number_int FROM test_dupl2) as foo
order by foo.number_int desc;

The wrong output:

('A', 1), ('B', 1)

The output i want:

('A', 9), ('B', 4)

Postgresql9.4

1 Answer 1

1
select distinct on (letter_one) letter_one, number_int 
from test_dupl2
order by 1, 2 desc;

 letter_one | number_int 
------------+------------
 A          |          9
 B          |          4
(2 rows)
Sign up to request clarification or add additional context in comments.

2 Comments

can you explain me this part "order by 1, 2" desc @klin ?
Order by first and second column, abbrev. for order by letter_one, number_int desc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.