How to calculate change in ANSI SQL
- by morpheous
I have a table that contains sales data. The data is stored in a table that looks like this:
CREATE table sales_data (
sales_time timestamp
, sales_amt double
)
I need to write parameterized queries that will allow me to do the following:
Return the change in sales_amt between times t2 and t1, where t2 and t1 are separated by a time interval (integer) of N. This query will allow for querying for weekly changes in sales (for example).
Return the change in change of sales_amt between times t2 and t1, and time t3 and t4. Thats is to calculate the value (val(t2)-val(t1)) - (val(t4)-val(t3)).
where t2 and t1 are separated by the same time interval (interval N) as the interval between t4 and t3.
This query will allow for querying for changes in weekly changes in sales (for example).