基于一列返回唯一行

时间:2014-04-11 17:30:11

标签: sql

我正在使用SQL Server 2008 ...

我有这个结果集:

ID        Name
------------------
717892    John
717892    Bill
717892    Steve
717200    Mark
717340    Gerald
717340    Frank

这就是我想要的:

ID        Name
------------------
717892    John
717200    Mark
717340    Frank

我不关心返回哪些特定行,我只需要根据ID列查看唯一的行

我试过了:

SELECT DISTINCT ID, * FROM #tempTable;

但是我收到以下错误:

The text data type cannot be selected as DISTINCT because it is not comparable.

我如何做到这一点?

1 个答案:

答案 0 :(得分:1)

;WITH cte AS
(
SELECT ID,Name,
       row_number() over (partition by id order by name) as row
From tableName
)

Select ID,Name
FROM cte
Where row=1