我正在使用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.
我如何做到这一点?
答案 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