A Method for SQL Calculation of four inter-line operations
The SQL language of the database can only calculate the sum between rows. To perform addition, subtraction, multiplication, and Division operations, we must find other operations to convert
Method. The method to convert subtraction to addition is relatively simple. You only need to first take the opposite number of The number to be subtracted, and then add it.
To transform multiplication into addition, we need to use mathematical knowledge. Assume that the common logarithm of A is m, and the M power of 10 is equal to a, expressed as: 10 ^
(Lg (A) =. The common logarithm of the product of two numbers AB is equal to the sum of the common logarithm of AB, expressed as: lg (A * B) = lg (A) + lg (B)
. This Inference
The concatenation product of N numbers A1, a2... an is equal to the M power of 10, where M = Σ (lg (AI )).
We can use lg (1/a) =-lg (A) to convert division into addition.
SQL> select sum (a) from (
2 (select power (10, sum (
3 case
4 when v2 = 1 then log (10, V1) -- Multiplication
5 when v2 = 2 then-log (10, V1) -- divide
6 end) A from TM where V2 <3)
7 Union all
8 (select sum (
9 case
10 when v2 = 3 then V1 -- add
11 when v2 = 4 then-V1 -- minus
12 end) A from TM Where v2> = 3)
13)
14;
Sum ()
----------
-. 6
SQL> select * From TM
2/
V1 v2
--------------------
5 2
2 1
3 3
4
SQL> select 1/5*2 + 3-4 from dual;
1/5*2 + 3-4
----------
-. 6