Skip to content
Advertisement

Select distinct values from a text column in PgAdmin [closed]

Using pgAdmin4 I imported text files to a table with 13 columns. The table columns are all of type text. The first column, named Date, contains text of dates (YYYY-MM-DD). There are multiple rows per date. I want to query and show the unique dates. I have used the following query from pgAdmin:

SELECT DISTINCT 'Date'
FROM "myTable"

The result from this query is just 1 result: Date. It does not include any data from a given cell.

enter image description here

I would expect the result to show:

2020-01-16
2020-01-15
2020-01-14

To try a simpler query. I queried:

SELECT 'Date'
FROM "myTable"

The query returns every row, but instead of the data (YYYY-MM-DD), each cell just says Date.

In following this PostgreSQL Tutorial I could not replicate the results.

I have noticed that with pgAdmin queries I wrap my columns and tables in single, or double quotes, whereas quotations are not included in many tutorials.

Advertisement

Answer

Remove the single quotes around 'Date', otherwise it’s a string litteral, not a column name.

SELECT DISTINCT Date FROM myTable

If you really need to quote the identifier, for example if it matches a reserved word, if it contains special characters or if the column was declared as case-sensitive, then Postgres wants double quotes:

SELECT DISTINCT "Date" FROM "myTable"
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement