![]() You can use SELECT with DISTINCT on two columns of the table: postgres=# select distinct col1,col2 from test order by 1 ĥ. DISTINCT can also be used with COUNT and other aggregates: postgres=# select col1,count(distinct(col1)) from test group by col1 Ĥ. SELECT with DISTINCT can also be used in an SQL inline query: postgres=# select col1 from (select distinct(col1) from test order by col1) as foo ģ. You can use SELECT with DISTINCT to find only the non-duplicate values from column “col1”: postgres=# select distinct(col1) from test order by col1 Ģ. Now that the table has been set up, let’s look at a few ways that DISTINCT can sort through the data:ġ. Use the SELECT query to see the rows of table “test”: postgres=# select * from test ![]() Insert a few records into table “test”: insert into test values (1,'abc','') Let’s set up a new table in PostgreSQL and use it to look at a few helpful ways that DISTINCT can remove duplicates and reveal useful information from the data.įirst, connect to psql terminal: /usr/pgsql-11/bin/psql -U postgres postgresĬreate a table, “test”: create table test(col1 int, col2 varchar,col3 date) Let's explore this keyword first.ĭISTINCT is used to remove duplicate rows from the SELECT query and only display one unique row from result set. ![]() One of the easiest ways to select distinct values is using the DISTINCT keyword. In this post, we are going to see how to select distinct values from SQL queries/statements. Keywords and clauses discussed with examples include: SUMMARY: This article reviews methods for selecting distinct values from query results in PostgreSQL.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |