How to calculate change in ANSI SQL

Posted by morpheous on Stack Overflow See other posts from Stack Overflow or by morpheous
Published on 2010-05-26T06:02:37Z Indexed on 2010/05/26 6:11 UTC
Read the original article Hit count: 305

Filed under:
|

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:

  1. 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).

  2. 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).

© Stack Overflow or respective owner

Related posts about sql

Related posts about ansi-sql