Select the first instance of a record Select the first instance of a record sql sql

Select the first instance of a record


You could use a CTE with ROW_NUMBER function:

WITH CTE AS(    SELECT myTable.*    , RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID)    FROM myTable )SELECT * FROM CTEWHERE RN = 1


It sounds like you're looking for DISTINCT:

SELECT DISTINCT patientID FROM myTable

you can get the same "effect" with GROUP BY:

SELECT patientID FROM myTable GROUP BY patientID


The simple way would be to add LIMIT 1 to the end of your query. This will ensure only a single row is returned in the result set.