Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Advanced SQL to update from insert
Message
 
 
À
25/01/2013 13:52:09
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Web
Divers
Thread ID:
01564084
Message ID:
01564107
Vues:
29
>>
>>;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
>>
>>
>>
>
>So, with that, as is, it will only returns a SQL result. If I start again from my SQL with a simplified version:
>
>
>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
>
>
>Where in there the cte syntax would apply?
;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
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform