Reputation: 197
Is it possible to get all unique records as well as their corresponding column in a database?
something like:
SELECT DISTINCT *
FROM table_name
?where?
I want to get all unique records with their corresponding column.
I tried:
SELECT distinct(column_name), other_column
FROM table_name
?where?
I still get duplicate records.
I tried:
SELECT distinct(column_name)
FROM table_name
?where?
I get unique records but incomplete column. How can I get all unique records w/ their column?
Upvotes: 0
Views: 101
Reputation: 92785
Are you looking for something like this?
SELECT t.*
FROM
(
SELECT MIN(pk_id) pk_id
FROM table_name
GROUP BY fk_id
) q JOIN table_name t
ON q.pk_id = t.pk_id
Here is SQLFiddle demo
In Postgres you can use DISTINCT ON
SELECT DISTINCT ON (fk_id) t.*
FROM table_name t
ORDER BY fk_id
Here is SQLFiddle demo
Upvotes: 1