How to Obtain Separate Values ​​from All Columns in a Table in SQL Server

advertisements

I want to retrieve record for distinct value with it's ID ,Table is like this ::

ID     Name
1      Soap
2      eridu
3      Soap
4      rewfig
5      Soap

Now I want result:

ID  Name
1   Soap
2   eridu
3   rewfig

I use like this: [Table Name is tblRecord

select Distinct(Name) ,ID
  from tblRecord
 where Name in (select Distinct(Name)
                  from tblRecord
               )


SELECT ID, Name
FROM
(
    SELECT ID, Name,
            ROW_NUMBER() OVER  (PARTITION BY NAME ORDER BY ID ASC) rn
    FROM tableName
) s
WHERE rn = 1
ORDER BY ID

but the simpliest Solution if you have no other column is

SELECT MIN(ID) ID, Name
FROM tableName
GROUP BY Name