Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Need to perform calculation with the previous record
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
00537434
Message ID:
00538537
Vues:
11
>>But I haven't of JOIN. I think that I can do something like:
>>INNER JOIN thetable b ON b.date = (select min(date) ... where c.date > a.date )
>>
>>Can this be good?
>
>this might be you're only option but I think you want a MAX() and not MIN(). You want the maximum date that is less than the date in the outer query.

I have reworked the query again to use a temp table. Is this a good idea? I have faster result but I don't if it is good.

This is my new query:
SET NOCOUNT ON 

if exists (select * from tempdb.dbo.sysobjects where name like '#WorkTable__%' and xtype ='U') 
	DROP TABLE #WorkTable 

SELECT HP1.BarraID, 
HP1.Date AS 'Date1', HP1.Price AS 'Price1', 
HP2.Date AS 'Date2', HP2.Price AS 'Price2', 
((HP2.Price / HP1.Price) - 1) * 100 AS Variation 
into #WorkTable 
FROM HistoPriceUSE3 AS HP1 
INNER JOIN HistoPriceUSE3 AS HP2 
ON HP1.BarraID = HP2.BarraID 
AND HP2.Date = (     SELECT MIN(Date)     FROM HistoPriceUSE3 AS HP3     Where HP3.BarraID = HP1.BarraID     AND HP3.Date > HP1.Date ) 
AND ABS((HP2.Price / HP1.Price) - 1) > 0.5 
WHERE HP1.Date >= CONVERT(DATETIME,'2001/07/15',101) 
AND HP2.ForceVariation IS NULL 

SELECT HP.BarraID, TT.Ticker, TT.Name AS Compagnie, 
HP.Date1 AS 'Dernière date', HP.Price1 AS 'Dernier prix', 
HP.Date2 AS 'Date courante', HP.Price2 AS 'Prix courant', HP.Variation 
FROM #WorkTable AS HP 
INNER JOIN Titre AS TT 
ON HP.BarraID = TT.BarraID 
AND TT.Modele = 'USE3' 
AND TT.DateLastUpdate = (     SELECT MAX(DateLastUpdate)     FROM Titre AS AA    Where AA.Modele = 'USE3'     AND AA.BarraID = HP.BarraID) 
ORDER BY HP.BarraID, HP.Date1 

SET NOCOUNT OFF
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform