Question : SQL Select statement question

I need to return a date, a Price and a Price from the previous date in a select statement. I am not sure how to do this.

I have a table that contains 3 fields
NodeDate, daPrice,rtPrice

NodeDate  is a SmallDateTime
daPrice is a Decimal(9,4)
rtPrice is a Decimal(9,4)

e.g.
NodeDate     daPrice      rtPrice
7/29/2010       2.3             2.4
7/28/2010       5                5.8
7/27/2010      2.5               0.4

I need to return NodeDate, (daPrice - rtPrice) as TodayDifference, (daPric-rtPrice this is YESTERDAY's data) as DifferenceFromYesterday

I think I could do this with something like
Select NodeDate, (daPrice-rtPrice) as TodayDifference, (Select Y.daPrice -Y.rtPrice From Table Y where Dateadd('d',-1,T.NodeDate) = Y.NodeDate) As DifferenceFromYesterday
From Table T

However, I do not think this is a very efficient method. There are close to 200,000,000 records that will be processed and I only want the Top 40 to be returned. Even if I add the TOP clause to the SQL statement, I am concerned that my version will take a long time to execute.

Does anyone have a suggestion?
thanks,
pat

Answer : SQL Select statement question

Update:  I just found the following article:
Finally, I found the answer to the problem. This hint sent me in the right direction. Even though the Welcome Screen and Fast User switching were OFF/DISABLED the all important registry key had not been changed. The following key was still set to "1" when I changed it to "0" offline files stopped unchecking itself after every reboot.
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]
"AllowMultipleTSSessions"=dword:00000000

This laptop had the setting to 1.  Going into User accounts in Control panel to look at the enable/disable setting produces a message stating that tvt_gina.dll has turned off welcome screen and fast user switching.  That belongs to Thinkvantage line of software.  Only this computer has the updated versions of software.

This resolved my issue.
Random Solutions  
 
programming4us programming4us