Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calculations Using SQL Query

BilalJul 5 2013 — edited Jul 8 2013

Hi All,

I am using Oracle Database Version 11.2.

I have a formula (A-B)/[(A-B)/10]. The tables that are holding the rows for this calculations are given below:

ROWS_TAB

====== ===================

Row          Amount

====== ===================

A            5000

B            -5000

FORMULA_TAB

======== =========== ======== =============

| ROW#  | Operator            | ROW        |   CONSTANT   |

======== =========== ======== =============

| 10        |     E                   |      A       |                         |

| 20        |     -                    |     B        |                         |

| 30        |     E                   |      A       |                         |

| 40        |     -                    |     B        |                         |

| 50        |     /                    |               |      10                |

| 60        |    /                     |               |                          |

======== =========== ======== =============

The operator E starts a new calculation. The above formula has two sub-expressions thereby the table has two rows with E. Minus denoted by -, Plus denoted by +, Multiply denoted by *, and Division denoted by /.

I want to write single SQL query to perform this calculation. Is it achievable in SQL?

The scripts used to generate the tables are as below:

create table ROWS_TAB

(row_name VARCHAR2(1),

amount NUMBER);

create table FORMULA_TAB

(row# NUMBER,

operator VARCHAR2(1),

row_name VARCHAR2(1),

constant NUMBER);

INSERT INTO ROWS_TAB VALUES('A', 5000);

INSERT INTO ROWS_TAB VALUES('B', -5000);

INSERT INTO FORMULA_TAB VALUES(10, 'E','A',null);

INSERT INTO FORMULA_TAB VALUES(20, '-','B',null);

INSERT INTO FORMULA_TAB VALUES(30, 'E','A',null);

INSERT INTO FORMULA_TAB VALUES(40, '-','B',null);

INSERT INTO FORMULA_TAB VALUES(50, '/','',10);

INSERT INTO FORMULA_TAB VALUES(60, '/','','');

Can anyone help in writing SQL query....

Thanks in advance

Best Regards

Bilal

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 5 2013
Added on Jul 5 2013
14 comments
4,215 views