SELECT
DISTINCT bcolor
FROM
distinct_demo
ORDER BY
bcolor;
-- NOTE: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
SELECT
DISTINCT ON (column1) column_alias,
column2
FROM
table_name
ORDER BY
column1,
column2;
Code language: SQL (Structured Query Language) (sql)
-- EXAMPLE
SELECT
DISTINCT on (client_id) client_id, id
FROM
public.dw_client_plan_histories
where operation = 4
and original_created_at >= '2022-06-17';
postgres=# select distinct on (col1) col1,col2,col3 from test order by col1;
col1 | col2 | col3
------+------+------------
1 | abc | 2015-09-10
2 | xyz | 2015-09-13
3 | tcs | 2015-01-15
(3 rows)