sql - performing calculations on a column for every 2 rows and displaying the result for the group -
i have table (current) extracted sql query. each benchmark has 2 data points. need calculate difference of 2 data points (i.e. more recent data point - less recent data point) , have result displayed alongside rows.
i guess has group by, weakest area in sql , not sure how return result display alongside grouped rows. i've maanged calculate difference doing "group benchmark" , have max(value) - min(value), realised dumb have no way of knowing signage. if can so, not know how display calculated value. assitance pls?
thanks. (oracle pls)
current datef benchmark value 31-jul-14 a 100 31-aug-14 a 101 31-jul-14 b 101 31-aug-14 b 99 31-jul-14 c 100 31-aug-14 c 101 31-jul-14 d 100 31-aug-14 d 100 31-jul-14 e 101 31-aug-14 e 102 either this: datef benchmark value difference 31-jul-14 a 100 1 31-aug-14 a 101 1 31-jul-14 b 101 -2 31-aug-14 b 99 -2 31-jul-14 c 100 1 31-aug-14 c 101 1 31-jul-14 d 100 0 31-aug-14 d 100 0 31-jul-14 e 101 1 31-aug-14 e 102 1 or this: datef benchmark value difference 31-jul-14 a 100 31-aug-14 a 101 1 31-jul-14 b 101 31-aug-14 b 99 -2 31-jul-14 c 100 31-aug-14 c 101 1 31-jul-14 d 100 31-aug-14 d 100 0 31-jul-14 e 101 31-aug-14 e 102 1
oracle offers first_value()
, last_value()
functions seem need. problem 1 of analytic/window functions, not aggregation.
select c.*, (last_value(value) on (partition benchmark order datef range between unbounded preceding , unbounded following) - first_value(value) on (partition benchmark order datef range between unbounded preceding , unbounded following) ) difference current c;
edit:
or, think can simplify above to:
select c.*, (first_value(value) on (partition benchmark order datef desc) - first_value(value) on (partition benchmark order datef) ) difference current c;
Comments
Post a Comment