Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

SQL-Server wählt bestimmte Zeilen mit Werten vor einem bestimmten Datum aus

DECLARE @REQUEST_DATE varchar(10)
SELECT @REQUEST_DATE = '1/9/2010'

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