A Method for SQL Calculation of four inter-line operations

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.