467,188 Members | 1,406 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,188 developers. It's quick & easy.

Calculating moving difference

Can some one please help me on this? I got 2 records by individual
with some fileds, one associates with the min date and the other one
assocaites with max date. So lay out looks as follows:

Key_ID | Dates_Min_Max Avg_Weight
1234 1/2/2004 12
1234 1/2/2006 24

I need to get the difference change between the weights for individual
ids that is group by Key_ID and find the percent change. Some thing
like this: (MAx_Row - Previos Row)/Previous Row * 100.

How to do this? How do I get the previos row between 2 records if I
order by the date? Any suggestions? Thanks

Jun 18 '06 #1
  • viewed: 1566
Share:
1 Reply
Sumon wrote:
I need to get the difference change between the weights for individual
ids that is group by Key_ID and find the percent change. Some thing
like this: (MAx_Row - Previos Row)/Previous Row * 100.


Whenever you compare values on two rows, you usually have to do a JOIN
to get them both on the same row of the result set, so you can compare
the values.

SELECT ((t2.avg_weight - t1.avg_weight) / t1.avg_weight) * 100
AS pct_difference
FROM tablename AS t1 JOIN tablename AS t2
ON t1.key_id = t2.key_id AND t1.dates_min_max < t2.dates_min_max;

Regards,
Bill K.
Jun 19 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Hans Gruber | last post: by
6 posts views Thread by Stephen Miller | last post: by
10 posts views Thread by riki | last post: by
3 posts views Thread by Ron Vecchi | last post: by
5 posts views Thread by Julius | last post: by
15 posts views Thread by mcjason@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.