|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
field calculation problem
Hello,
Can anybody help me with a SQL query problem which I have. I have 2 fields which I would like to add another 2 more fields with a calculation, one field calculates with a level return, the other is a 10% increase or decrease. For example the table will hold the following data Field_A : Field_B 1 : 1.4 2 : 1.7 1 : 1.3 5 : 1.0 3 : 1.0 I would like to only add field A if the result is 1 else I will want to subtract 1 . So I have an SQL statement which looks look like select Field_A, Field_B, SUM(IF(Field_A=1, Field_B.-1)) AS Field_C from mytable group by Field_A ; Field_A : Field_B: Field_C 1 : 1.4 : 1.4 2 : 1.7 : 0.4 1 : 1.3 : 1.7 5 : 1.0 : 0.7 3 : 1.0 : -0.3 What I would like to do for the next field (Field_D) is have it count up using a 10% increments for example: If Field_A=1 then I would like take a Field_B and multiply it by a 10% of the bank total. If Field_A does not equal 1 then decrease the bank by 10 % Field_D starts at 1 units Field_A : Field_B: Field_C: Field_D 1 : 1.4 : 1.4 : 1.14 2 : 1.7 : 0.4 : 1.03 1 : 1.3 : 1.7 : 1.16 5 : 1.0 : 0.7 : 1.04 3 : 1.0 : -0.3 : 0.94 Any advise people can give me will greatly be appreciated. regards Stevie D. |
|
#2
|
|||
|
|||
|
Do you mean something accumulative?
On row 1, Field_C = Field_B. On row n, if Field_A == 1, then Field_C = Field_A + (Field_C of row (n-1)), else Field_C = -1 + (Field_C of row (n-1)); Do you have to keep this in the database? Can't you just calculate it when fetching the data? The values of Field_D I don't understand. ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Databases > General SQL Development > field calculation problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|