Home All Groups Group Topic Archive Search About

time_series calculations in SQL

Author
23 Jun 2006 12:19 AM
GB
Hello,
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

Author
23 Jun 2006 12:52 AM
Lubdha Khandelwal
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)

AddThis Social Bookmark Button