Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 5123

How to select distinct record?

$
0
0

Hi,

 

I have a table, as follows:

 

ID  |  Name | MyID

1    |     Alan | 2

2    |    Alan   | 2

3    |   John   |  3

 

 

I want to select the records as follows:

 

  1. If two records have the duplicate values in ‘Name’ column, then only the first record is selected and the other record is discarded, even if the two records have different values in other columns.
  2. If two records have different ‘Name’ value, then they are ordered via ‘MyID’ column in ascendant order.
  3. All columns of the records should be returned.

 So for the above records, the query should select the following ones:

ID  |  Name | MyID

1    |     Alan | 2

3    |   John   |  3

How to write the query? It seems I should use DISTINCT or GROUP BY, but my queries are all fail. Please help.

 

Thanks



Viewing all articles
Browse latest Browse all 5123

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>