Skip to content
Advertisement

distinct clause doesn’t produce expected result

I’d like to have one row for each different hadm_id this is my query :

the result is not what I am expecting:

How can I do to fix it.

Advertisement

Answer

SELECT DISTINCT operates on the whole dataset, not a particular column. So you get one record per distinct combination of all values in all columns returned by the query.

Since you are using Postgres, you might want to try DISTINCT ON. This gives you one record per group defined in the ON clause. The ORDER BY clauses determines which row should be returned in each group.

In your query:

This would give you one record per hadm_id, with the record that has the smallest intime. You would need to adapt the order by clause with the correct column for your use case.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement