>>;with cte as (select cast(Version.[Date] as Date) as [DateNoTime], *, >>COUNT(*) OVER (partition by cast(Version.[Date] as Date)) as cntVersionsPerDay, >>row_number() over (partition by cast(Version.[Date] as Date) ORDER BY Version.[Date] DESC) as Rn >> from dbo.Version) >> >>insert into VersionProduction (AddUser, AddDate, ....) >> >>select .... >> >>from cte where Rn = 1>