最近更新的项目来自 Table
Last Updated Item from Table
UniqueIndex Item
521 ABC
520 ABC
519 ABC
518 ABC
517 CDC
516 CDC
515 CDC
需要 T-SQL 以获得结果
521 ABC
517 CDC
对于这个 2 列数据集,您可以只使用聚合:
select item, max(uniqueIndex) lastUniqueIndex
from mytable
group by item
如果要显示更多列,则可以使用子查询进行过滤:
select t.*
from mytable t
where t.uniqueIndex = (select max(t1.uniqueIndex) from mytable t1 where t1.item = t.item)
为了此处的性能,请考虑 (item, uniqueIndex)
上的索引。
您还可以使用 window 函数:
select *
from (
select t.*, row_number() over(partition by item order by uniqueIndex desc) rn
from mytable t
) t
where rn = 1
UniqueIndex Item
521 ABC
520 ABC
519 ABC
518 ABC
517 CDC
516 CDC
515 CDC
需要 T-SQL 以获得结果
521 ABC
517 CDC
对于这个 2 列数据集,您可以只使用聚合:
select item, max(uniqueIndex) lastUniqueIndex
from mytable
group by item
如果要显示更多列,则可以使用子查询进行过滤:
select t.*
from mytable t
where t.uniqueIndex = (select max(t1.uniqueIndex) from mytable t1 where t1.item = t.item)
为了此处的性能,请考虑 (item, uniqueIndex)
上的索引。
您还可以使用 window 函数:
select *
from (
select t.*, row_number() over(partition by item order by uniqueIndex desc) rn
from mytable t
) t
where rn = 1