Chi,
There may be one problem with GalaxiomAtHome's solution.
you may end up with too many rows.
Here is a sample of the SQL he might have been proposing
SELECT
a.month_no - b.month_no as month_diff
, a.total_of_sales - b.total_sales as sales_diff
, a.total_prod - b.total_prod as prod_diff
from
sales_table as a
, sales_table as b
where a.month_no > b.month_no
the problem with this is that you not only
get the two adjenct rows, but for
the third row you get the difference
between it and the first row, etc.
What you need to do is create a new
"view of the table" where each row
has a new column that identifies
the previous row, then join
that "view" of the two tables to
do the difference.
Here is a modification that may do just that.
(Ignore the "periods", I put them in there to try and maintain spacing
to make the query more readable).
The "view" is the embedded "SELECT" statement
enclosed in "(..)". You can place this select
statement in a separate query, and then include it
by replacing the "(SELECT...)" with the name of the query.
SELECT
a.month_no - b.month_no as month_dif
, a.total_sales - b.total_sales as sales_dif
. a.total_prod - b.total_prod as total_dif
from
sales_table as a
, (select
m.month_no as cur_month
, max(p.month_no) as prev_month
from sales_table as m
, sales_table as p
where m.month_no > p.month_no
group by m.month_no
) as j
, sales_table as b
where a.month_no = j.cur_month
and b.month_no = j.prev_month
(I wish this tool did not eliminate the extra spaces. I think the spaces makes it easier to read and see the structure.)
With this result you will only get n-1 rows
for a table of n rows.
There are other ways to write the SQL to do the same thing,
which might be more "formal" and
work faster. I just wanted to
make this easy for you to understand what is happening.
Hope this helps.