tsq - 获取重复行中的最高int

时间:2015-03-03 19:12:13

标签: sql-server tsql

我在表格中有以下行

name, tagid
-------
test1,1
test1,100
test2,2
test2,200
test3,3
test3,300

名称中有重复项。 有没有办法通过获取每个组的最高tagid来选择唯一名称?

2 个答案:

答案 0 :(得分:2)

;WITH cte AS
(
   SELECT *, ROW_NUMBER() OVER (PARTITION BY name ORDER BY tagid DESC) AS rn
   FROM table_1
)
SELECT *
FROM cte
WHERE rn = 1

答案 1 :(得分:2)

select name,max(tagid) as highest_tagid
from tbl
group by name