Dasaya_Developer
Dasaya_Developer

Reputation: 67

ORDER table data using 'DISTINCT'

Im using postgress pgadmin as my database. In my application I need to search details from a specific table.

For that I used method like below.


SELECT DISTINCT xx , yy
FROM (
  SELECT xx , yy
) total

Here I used DISTINCT to remove duplicate entries. But when I use that, it will sort the filtered data according to tableID.

So is there any method to remove duplicates & filetere data without sorting them according to tableID ?

Upvotes: 0

Views: 75

Answers (1)

user18098820
user18098820

Reputation:

If you do not specify any order the rows will generally be returned in the order that they were inserted into the table, but this is not garanteed.
You can specify the order with ORDER BY as you need

  • sort by xx then yy, with ORDER BY xx, yy or the opposite with ORDER BY yy, zz
  • sort smallest first by default or with ASC
  • sort largest first with DESC
    for example ORDER BY xx DESC will return the row with the largest value of xx first.
  • sort by functions for example ORDER BY x / y
SELECT DISTINCT xx , yy
FROM (
  SELECT xx , yy
) total
ORDER BY xx , yy;

Upvotes: 1

Related Questions