sql >> Base de Datos >  >> RDS >> Sqlserver

El servidor SQL selecciona filas distintas usando solo el valor más reciente

Una forma

select t1.* from (select ForeignKeyId,AttributeName, max(Created) AS MaxCreated
from  YourTable
group by ForeignKeyId,AttributeName) t2
join YourTable t1 on t2.ForeignKeyId = t1.ForeignKeyId
and t2.AttributeName = t1.AttributeName
and t2.MaxCreated = t1.Created

Consulte también Incluir los valores relacionados de una columna agregada para ver 5 formas diferentes de realizar este tipo de consulta