>>;with cte as (select NoVersionProduction, cast(Version.[Date] as Date) as [Date], >>row_number() over (partition by cast(Version.[Date] as Date) ORDER BY Version.[Date] DESC) as Rn >> from dbo.Version) >> >>select NoVersionProduction, [Date] -- without time portion >>from cte where Rn = 1 -- this will give you a row with latest date per each date >> >> >>>
>INSERT INTO VersionProduction (AddUser,AddDate,ModUser,ModDate,Date,Completed,Started,Ended,Synchronization,Note, > NoteForAdministrator,NoteForDBA,NoteForProductManager) >SELECT 958,Temp.Date,958,Temp.Date AS Date2,Temp.Date AS Date3,1,Temp.Date AS Date4,Temp.Date AS Date5,'','','','','' FROM >(SELECT CAST(Version.Date AS Date) AS DateTemp,MAX(Version.Date) AS Date,COUNT(*) AS NoName2 > FROM Version WHERE Date>'1899-12-30 00:00:00.000' > GROUP BY CAST(Version.Date AS Date)) Temp >ORDER BY Date DESC >>
;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