Friday, February 10, 2012

calculated value based on values in other row

hello,
in a table i have 3 fields
custnr wnr profit
example
1 1 100
1 2 110
1 3 120
1 4 150
i want to calculate how much the profit grows referenced from wnr 1 and
the relative to the precedent w
1 1 0% 0%
1 2 10% 10%
1 3 20% 10%
1 4 50% 30%
thanksXavier wrote:

> i want to calculate how much the profit grows referenced from wnr
> 1 and the relative to the precedent w
> 1 1 0% 0%
> 1 2 10% 10%
> 1 3 20% 10%
> 1 4 50% 30%
This should work:
select profit/ (select top 1 TB2.profit from table TB2 where TB2.wnr
< TB1.wnr order by wnr DESC) from table TB1
HTH,
Stijn Verrept.|||thanks for your help,
in the table there are a lot of custnr.
The corect form of the example table is analog to the custNr 1 there are
also Nr like
custnr wnr profit
example
1 1 100
1 2 110
1 3 120
1 4 150
2 1 100
2 2 120
2 3 140
2 4 160
3 1 100
3 2 120
3 3 130
3 4 140
...
how can i extend to have a corect calculation for all customer
best regards
"Stijn Verrept" wrote:

> Xavier wrote:
>
>
> This should work:
> select profit/ (select top 1 TB2.profit from table TB2 where TB2.wnr
> < TB1.wnr order by wnr DESC) from table TB1
> --
> HTH,
> Stijn Verrept.
>|||Xavier wrote:

> ...
> how can i extend to have a corect calculation for all customer
select profit/ (select top 1 TB2.profit from table TB2 where TB2.wnr
< TB1.wnr and TB2.custnr
= TB1.custnr order by wnr DESC) from table TB1
HTH,
Stijn Verrept.|||yes, it works, thanks
best regards
Xavier

No comments:

Post a Comment