|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
time_series calculations in SQLHello,
I have a simple table of 2 columns: datadate ( datetime) and x (real). Is it possible to create SQL select statement ( not UDF) which performs the following calculations: y = (x(t) - x(t-12)) / x(t-12). Here t is datadate column value. Thanks, GB Assuming t-12 implies that you're trying to diff the MONTH part of a
datetime, how about something like: SELECT ( ((CASE WHEN T1.X IS NOT NULL THEN T1.X ELSE 0.0 END) - (CASE WHEN T2.X IS NOT NULL THEN T2.X ELSE 0.0 END)) /(CASE WHEN T2.X IS NOT NULL THEN T2.X ELSE 1.0 END) ) AS Diff FROM Table T1 LEFT OUTER JOIN Table T2 ON (DATEPART(MM, T1.T) = DATEPART(MM, T2.T) + 12) |
|||||||||||||||||||||||