Database query in SQL to select first unique record

Here’s a quick code snippet to create a query that selects all unique records from field1, and selects the first corresponding record in field2

SELECT DISTINCT field1, First(field2) AS field2first
FROM table1
GROUP BY field1;

The function First, can be substituted for Last, Min, Max, depending on desired returned record. In the example above, table1 is the recordset, field1 is the field which you want unique records only, and field2 are the corresponding records, field2first is just an alias.


Posted

in

by

Comments

One response to “Database query in SQL to select first unique record”

  1. SQL Tutorials Avatar
    SQL Tutorials

    You know, the thing about SQL is, that there is virtually nothing that can replace it.

    Does anyone know if a substitute exists for sql? I mean besides MS SQL and Oracle and all that jazz. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *