Skip to content
Advertisement

How to select only the first rows for each unique value of a column?

Let’s say I have a table of customer addresses:

In the table, one customer like John Smith can have multiple addresses. I need the SELECT query for this table to return only first row found where there are duplicates in ‘CName’. For this table it should return all rows except the 3rd (or 1st – any of those two addresses are okay but only one can be returned).

Is there a keyword I can add to the SELECT query to filter based on whether the server has already seen the column value before?

Advertisement

Answer

A very simple answer if you say you don’t care which address is used.

If you want the first according to, say, an “inserted” column then it’s a different query

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